It should be specified that the variable seems to be deleted when the figure is closed, and I want to access the data..
Share variables between callbacks and save when GUI window is closed
2 views (last 30 days)
Show older comments
Hi. I am developing a larger GUI, but have made a simplified part of it to make the problem easier. In one uicontrol I want the user to be able to define some values that should be accessed later in the GUI. However, it seems that if I define a value in the while loop it can not be accesssed in a callback function. Any tips on how to make this work?
clc;
clear all;
close all;
end
S.d = dialog('Position',[200 200 800 200],'Name','My Dialog',...
'WindowStyle','normal');
S.btn = uicontrol('Parent',S.d,...
'Position',[200 20 70 25],...
'String','Add',...
'Callback',{@mydef_function,S});
S.btnadd = uicontrol('Parent',S.d,...
'Position',[400 20 70 25],...
'String','Save',...
'Callback',{@mysave_func,S});
S.btnexit = uicontrol('Parent',S.d,...
'Position',[600 20 70 25],...
'String','Close',...
'Callback',{@my_exit_function,S});
bOK = true;
while(bOK)
uiwait(S.d)
guidata(S.btnadd)
if ~isempty(S.btn.UserData)
bOK=false;
end
end
function mydef_function(src,event,S)
uiresume(S.d)
val = 2;
guidata(src,val)
end
function mysave_func(src,event,S)
uiresume(S.d)
S.val = guidata(S.btn)
guidata(src,S.val)
end
function my_exit_function(src,event,S)
uiresume(S.d);
src.UserData = 1;
end
2 Comments
Adam
on 22 Jan 2018
When using guidata you can only have one thing saved as guidata. Subsequent calls to guidata with an argument to save will overwrite the previous one. This sounds useless as stated, but guidata is generally used within GUIDE with the 'handles' struct containing all the GUI components being the thing that is always saved, often after having first being retrieved and added to since you can attach any custom fields you want to the struct in addition to GUI handles.
In your case you don't have 'handles' so you would need to create your own struct to use guidata and ensure that you always save the same one and that you retrieve it with e.g.
handles = guidata( src );
handles.val = 2;
guidata( src, handles )
to add a value to it and put it back in the GUI without losing the other data that was also stored on it.
As per Walter's answer though, this data will be linked to the lifetime of the GUI still.
Accepted Answer
Walter Roberson
on 22 Jan 2018
Note that dialog() creates a new figure, so when you use guidata(src, ...) in the callbacks for uicontrol on that dialog figure, you are storing data against the dialog figure, and that data is going to disappear when the figure is closed. You should have a CloseRequestFcn callback which should store the data somewhere that the other code can get at it.
inputdlg() and questdlg() with this slightly differently: they store the data against the dialog figure they create, and declare that if the dialog figure gets deleted that the input failed; if it was not deleted at the time they leave the uiwait() due to the uiresume() then they fetch the data from the dialog figure and then destroy the dialog figure.
3 Comments
Walter Roberson
on 22 Jan 2018
inputdlg() and questdlg() have no ability to use uitables. Both of them are routines that construct dialog figures and set up appropriate text on the page and appropriate uicontrol and callback routines, and then do uiwait(), fetch the data from the dialog figure, and return the data. You can read their source code.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!