MATLAB: GUI data is getting deleted after execution of push button command (GUIDE)

guidematlab guivariableworkspace

I am making a GUI in GUIDE, for instrument automation. I am using a push button call "measure" to execute a set of command, but as soon as the command is over all my variables stored in workspace is getting deleted.
I need this variable for my further calculation.
Please help me to figure it out a solution.

Best Answer

Pooja - if you are initializing local variables within your callback function like
function pushbutton1_Callback(hObject, eventdata, handles)
x = 3;
y = 4;
z = 4;
t = 3*x + 2*y + z;
% etc.
then so long as the function is being evaluated, the (function) workspace will be populated with the hObject, eventdata, handles, x, y, z, t, etc. variables. When the function completes and so program control leaves the function, then these variables are considered out of scope and so are no longer available.
If you wish to have access to some of these variables (that you have created in your function) then save them to the handles structure so that they the other callbacks can refer to them.
In the above example, suppose I want to save t so that another (different) callback has access to it. I would do
function pushbutton1_Callback(hObject, eventdata, handles)
x = 3;
y = 4;
z = 4;
t = 3*x + 2*y + z;
% do something else with t
% save it handles
handles.t = t;
guidata(hObject, handles);
Note that the guidata is important as it saves the updated handles structure.