MATLAB: GUI Inputs Say Undefined but show in workspace

guirecall variables

The user defines the inputs M1, t1, M2, and t2 in the GUI. Once the 4 inputs are defined from click buttons and text inputs, the workspace values are updated (I know this as the original values are set to 999 each and they change to the updated values). However, when I try and run a function at a later step in the GUI (with a click button set to be the run function) with those 4 inputs, it says they are not defined. Do I need to recall the 4 inputs inside of the push-button run function? And if so, what is the best approach? Any suggestions would be great!
Thanks

Best Answer

Jamie - if you have created your GUI using GUIDE, use the handles structure to store your intermediate data that has been calculated within one callback that needs to be available in another callback. You would use the guidata function to set (and retrieve) user-defined data to the GUI.
For example, suppose I have two push buttons. The first does some sort of calculation which is needed when I press the second push button.
function pushbutton1_Callback(hObject, eventdata, handles)
% do a calculation
myCalculation = ...;
% save this calculation to the handles structure
handles.myCalculation = myCalculation;
guidata(hObject,handles);
Now in your second push button callback, you would do
function pushbutton2_Callback(hObject, eventdata, handles)
if isfield(handles,'myCalculation')
% do some other calculation (or pass into another function)
myCalculation = handles.myCalculation;
% etc.
end
This is way of sharing variables between callbacks is preferred over polluting the workspace with variables.