How to save 2 variables from function and use it in another function in MATLAB GUI?

1 Ansicht (letzte 30 Tage)
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.

Akzeptierte Antwort

Paulo Silva
Paulo Silva am 22 Jun. 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)

Weitere Antworten (1)

Laura Proctor
Laura Proctor am 22 Jun. 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.
  1 Kommentar
Muhammad Asad
Muhammad Asad am 22 Jun. 2011
I want to save what's written in a textbox when the first button is pressed. I want to save what's written in another textbox when the second button is pressed. Then when the third button is pressed, I want to use both of those variables. How to do it?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Workspace Variables and MAT-Files finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by