How to save 2 variables from function and use it in another function in MATLAB GUI?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I have made a MATLAB GUI which has 3 buttons. I want to perform some tasks on CallBack event of all these buttons.
I have obtained a variable var1 when the first button is pressed. And obtained another variable var2 when the second button is pressed. But as soon as the function of first and second button ends, the variables get deleted.
I want to use these variables in the third button. Since, the first and seconds function variables are deleted as soon as the function ends, how can I use these variable within the third button's function.
I can save one variable through using this code in first button function:
mydata.value = var1; setappdata(gcf,'mydata',mydata);
And calling it in third button function
mydata = getappdata(gcf, 'mydata');
But how can I save two variables using this process?
I tried using following in second button code but it didn't work:
mydata1.value = var2; setappdata(gcf,'mydata1',mydata1);
Thank you.
0 commentaires
Réponse acceptée
Paulo Silva
le 22 Juin 2011
%callback of button 1
var1=get(handles.text1,'String')
set(handles.text1,'String','')
handles.var1 = var1;
guidata(hObject,handles)
%callback of button 2
var2=get(handles.text2,'String')
set(handles.text2,'String','')
handles.var2 = var2;
guidata(hObject,handles)
%callback of button 3
var1=handles.var1
var2=handles.var2
%if you want numbers instead of strings do var1t=str2double(var1)
Plus de réponses (1)
Laura Proctor
le 22 Juin 2011
If you are using GUIDE to create your GUI, then try using the guidata function with the handles structure.
For example, save your variables in handles:
handles.var1 = var1;
guidata(hObject,handles)
If you didn't use GUIDE to create your GUI, then you can still use the guidata function to retrieve and save a structure of data from function call to function call. There's an example in the doc that is linked above.
Voir également
Catégories
En savoir plus sur Workspace Variables and MAT Files dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!