Print text in my own function in GUIDE GUI using "handles"

84 Views Asked by At

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);
1

There are 1 best solutions below

4
On BEST ANSWER

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