Why GUI data is getting deleted after execution of push button command (GUIDE) ?
1 view (last 30 days)
Show older comments
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.
1 Comment
Adam
on 4 Jul 2017
"execute a set of command"
Well, fairly obviously those commands are what is causing this so if you aren't going to show us any specific code there's not a lot we can do.
People often seem to like to include instructions like
clear all
close all force
and other equally silly things in GUIs which can cause unpleasant behaviour.
Accepted Answer
Geoff Hayes
on 4 Jul 2017
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);
3 Comments
More Answers (1)
Jan
on 4 Jul 2017
all my variables stored in workspace is getting deleted
This leaves to details unclear:
- Which workspace
- How are the variables deleted and why?
Remember that each function has its own workspace. If you create a variable in the workspace of a callback, leaving the callback will and must delete them. If you need them anywhere else, store the results:
function Buttoncallback(hObject, EventData, handles)
handles = guidata(hObject); % Safer to get the current value
[X, Y] = YourFunction();
handles.X = X;
handles.Y = Y;
guidata(hObject, handles); % Store current version in figure again
end
Now all callbacks can access the outputs of the function. You could add an "Export" button, whose callback uses assignin('base', 'X', handles.X) to export the values to the base workspace ("the command window").
6 Comments
doodle
on 4 Jun 2018
Edited: doodle
on 4 Jun 2018
Thanks both
Nope, "clear all" not used in the code, and I had already used guidata, although thanks for the suggestions. It did in fact turn out to be a similarly silly mistake, though...
The variables from the later script weren't getting deleted from the base ws [see below] ... they just hadn't been created due to a while-loop condition not being met ... which in turn occurred because of a trivial typo in declaring a variable within the GUI script [embarrassed face]. Always the way...... Sorry to have wasted your time, thanks for the responses!
Note for anyone else working at my fairly basic level:
Technically the variables declared in the other script were being deleted, but only at the very end, because the script had itself been invoked within the pushbutton callback function which clears its variables once finished. [Also this was not the cause of the problem above, as explained.]
For anyone else who finds this behaviour inconvenient, you can call the GUI from the other script rather than vice versa. In that approach, though, you'll then have to make your other script wait for the GUI pushbutton press, documented elsewhere. You'll also have to use 'assignin' (see Jan's answer) to pass the variables to your controlling script (if trying to use guidata as outlined above by various users for the OP's case, the variables will still remain local to the GUI script and so, in this case, can't be called from your controlling script once the GUI script has finished). By contrast, calling the other script from the GUI pushbutton callback doesn't incur these problems, if you don't mind your variables disappearing from the base workspace once everything has finished. Which is probably 'best practice' anyway judging from other threads.... people don't tend to explain "why" this is best practice, but I'm guessing it has to do with the danger of obsolete variables hanging around the workspace to clash with future ones of the same name.
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!