0

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?

Jonas Heidelberg
  • 4,984
  • 1
  • 27
  • 41
Kyle Uithoven
  • 2,414
  • 5
  • 30
  • 43

1 Answers1

3

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.

CitizenInsane
  • 4,755
  • 1
  • 25
  • 56
  • Thank you very much for your quick feedback. Do both of these cases still work if functions are contained in separate .m files? – Kyle Uithoven Apr 05 '11 at 21:22