Why GUI data is getting deleted after execution of push button command (GUIDE) ?

1 次查看(过去 30 天)
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 个评论
Adam
Adam 2017-7-4
"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.

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2017-7-4
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.
  3 个评论
Pooja Mehta
Pooja Mehta 2017-7-17
编辑:Pooja Mehta 2017-7-17
@Geoff What I was missing is to use "guidata(hObject, handles)" command after storing data in particular variables. Thank you for insight.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2017-7-4
all my variables stored in workspace is getting deleted
This leaves to details unclear:
  1. Which workspace
  2. 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 个评论
Pooja Mehta
Pooja Mehta 2018-6-4
编辑:Pooja Mehta 2018-6-4
@doodle
As @Geoff has explained
for storing the user input data first of all you have to store the data in handles structure.
handles.t = t
Where t is any variable you want to store. And then update GUI data with the command
guidata(hObject, handles)
storing your data in handles is important, then the only guidata command will store your variable and update.
doodle
doodle 2018-6-4
编辑:doodle 2018-6-4
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by