amkas90
amkas90

Reputation: 139

Passing variables between function in MATLAB GUI using handles

I am relatively new to MATLAB so forgive me if this is rather basic question. I am trying to understand how to manipulate variables and pass the results between functions within the GUI.

If I set up the GUI using the GUIDE interface I get several functions. I would like to do a certain action when I click a pushbutton, save a variable, then use that variable in another function.

function pushbutton1_Callback(hObject, eventdata, handles)
     handles.MyData = 7;

Now, since that data is stored in handles can I not simply use this in another function within the GUI in this fashion?

function pushbutton2_Callback(hObject, eventdata, handles)
     result = 5 + handles.MyData;

This is a really simple example, but I am trying to get down to the logic of how to pass variables. I know there is a lot of information out there and I have read it but I cannot get down to the logic of how the variables are stored in the structure and how they can be passed between functions.

Upvotes: 0

Views: 2564

Answers (1)

ThP
ThP

Reputation: 2342

When you update the handles struct, you have to store it using guidata:

guidata(hObject, handles);

Then you can use it in a different callback.

Upvotes: 3

Related Questions