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
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
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);
댓글 수: 3
추가 답변 (1개)
Jan
2017년 7월 4일
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
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 Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!