Kyle Uithoven
Kyle Uithoven

Reputation: 2454

How do I access controls on my GUIDE figure from other functions?

I am using GUIDE to create a GUI for my MATLAB project.

In one of my callbacks for a button, I call a function.

[Name]= otherFunction(inputVariable);

set(handles.name,'String',Name);

After I receive the output from that function, I set the name label to the value of Name. Is it possible to set that from inside the function? What do I have to do to allow that function to access the GUIData?

I have tried using set/get from inside that function but I can't seem to get it to work.

Alternatively, is there anyway that I can make the 'handles' globally available?

Upvotes: 0

Views: 935

Answers (1)

CitizenInsane
CitizenInsane

Reputation: 4875

Starting from a blank GUI and simply adding a pushbutton to it (tagged as 'btnTest'), the following code works fine:

%% --- Executes on button press in btnTest.
function btnTest_Callback(hObject, eventdata, handles)
%[
    changeName(handles);
%]

%% --- Inner function
function [] = changeName(handles)
%[
    set(handles.btnTest, 'String', 'toto'); 
%]

So there's probably something else wrong in your code.

If you intend not to pass the 'handles' structure to the 'changeName' function (i.e. have handles globally available), you can do it like this:

%% --- Executes on button press in btnTest.
function btnTest_Callback(hObject, eventdata, handles)
%[
    changeName();
%]

%% --- Inner function
function [] = changeName()
%[   
    handles = guihandles(); % recover handles for current figure
    set(handles.btnTest, 'String', 'toto'); 
%]

But it's much slower than passing 'handles' directly.

Upvotes: 3

Related Questions