-1

I want to create my own function in MATLAB which check some conditions but I don't know how to send handles in there. In the end, I want to print some text in the GUI from this other function. I can't use handles.t1 directly in this function because it is not accessible from within the function. How can I pass it there?

function y = check(tab)
    if all(handles.tab == [1,1,1])
            set(handles.t1, 'String', 'good');
        else
            set(handles.t1, 'String', 'bad');
    end
end

Edit

After comment and first answer I decided to put whole callback where I call my function:

function A_Callback(hObject, eventdata, handles)
if handles.axesid ~= 12
    handles.axesid = mod(handles.axesid, handles.axesnum) + 1;
    ax = ['dna',int2str(handles.axesid)];
    axes(handles.(ax))
    matlabImage = imread('agora.jpg');
    image(matlabImage)
    axis off
    axis image
    ax1 = ['dt',int2str(handles.axesid)];
    axes(handles.(ax1))
    matlabImage2 = imread('tdol.jpg');
    image(matlabImage2)
    axis off
    axis image
    handles.T(end+1)=1;
    if length(handles.T)>2
        check(handles.T(1:3))
    end
end
guidata(hObject, handles);
Suever
  • 64,497
  • 14
  • 82
  • 101
shurrok
  • 795
  • 2
  • 13
  • 43
  • "My own function" could you be *more* ambiguous? Is this part of a programmatic GUI? GUIDE GUI? Random MATLAB function? Class definition? Script? How is this function being called? See: [mcve] – sco1 Jan 04 '17 at 18:47
  • Yes, this is GUIDE GUI. When i was writting "my own function" i thought it's something different than callback function. This function is called "check" it is checking if the array has got the same values as you can see in "if". What I need is knowledge how can I use "handles.(whatever_here)" in my own declared function. – shurrok Jan 04 '17 at 19:05

1 Answers1

2

You'll need to use guidata to retrieve the handles struct that GUIDE automatically passes between callbacks. You'll also need a handle to the figure to retrieve the guidata and we'll use findall combined with the Tag property (below I've used mytag as an example) to locate the GUI figure.

handles = guidata(findall(0, 'type', 'figure', 'tag', 'mytag'));

If the input argument tab is a handle to a graphics object within your figure, you can just call guidata on that to get the handles struct

handles = guidata(tab);

Update

In your update to your question, since you are calling check directly from a callback, simply pass the necessary variables to your function and then operate on them normally

function y = check(tab, htext)
    if all(tab == [1 1 1])
        set(htext, 'String', 'good')
    else
        set(htext, 'String', 'bad')
    end
end

And then from within your callback

if length(handles.T) > 2
    check(handles.T(1:3), handles.t1);
end

Alternately, you could pass the entire handles struct to your check function

function check(handles)
    if all(handles.tab == [1 1 1])
        set(handles.t1, 'string', 'good')
    else
        set(handles.t1, 'String', 'bad')
    end
end

And then within your callback

if length(handles.T) > 2
    check(handles)
end
Graham
  • 7,431
  • 18
  • 59
  • 84
Suever
  • 64,497
  • 14
  • 82
  • 101
  • I don't get the idea at all. I've updated the question. Can you give me more detailed answer, specified to my problem? – shurrok Jan 04 '17 at 19:18
  • 1
    @Buszman Do you know the `Tag` property of your GUIDE gui? You should be able to set it in the properties. Alternately, I have provided an update based on your update that shows you how to just pass the info you need to your `check` function directly. – Suever Jan 04 '17 at 19:22
  • i don't know the Tag property. How can I check it? I suppose I need to pass entire handles because I've got four text boxes(t1, t2, t3 and t4) – shurrok Jan 04 '17 at 19:25
  • @Buszman Use the property editor within GUIDE and select the figure. There is a `Tag` property that you can set to any value you wish. – Suever Jan 04 '17 at 19:28