Share variables between callbacks and save when GUI window is closed
Mostrar comentarios más antiguos
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 comentarios
proczell
el 22 de En. de 2018
Adam
el 22 de En. de 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.
Respuesta aceptada
Más respuestas (1)
Birdman
el 22 de En. de 2018
0 votos
6 comentarios
proczell
el 22 de En. de 2018
Birdman
el 22 de En. de 2018
Which version of MATLAB are you using?
proczell
el 22 de En. de 2018
Birdman
el 22 de En. de 2018
Well, in that case, I suggest you to use App Designer. It is so much easier to built applications rather than using old fashioned way. I built an application for you where you can plot a vector dependent of a vector that you enter, for instance
[1 2 3 4 5]
Now, what I want you to do is to run the application. Firstly, click on Plot Properties On button to do the necessary settings for the plot named Title. Then, in the Edit Field part, enter
1,3,5,7,9
exactly with commas as I wrote and then click on Plot. The output is calculated as
output=2*input-1;
Then, click on Mean of Output button, which is a different callback in the application. In the below numeric field, you will see the mean of the output vector.
After first trial, go to Code View of the app and see how I stored the output from first callback button. It is stored in a struct of the app. Check the notations and other stuff.
proczell
el 22 de En. de 2018
Birdman
el 22 de En. de 2018
Ok, let me know the results. As I said, it is really easy to do that kind of stuff in App Designer.
Categorías
Más información sobre App Building en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!