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

1 次查看(过去 30 天)
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.

采纳的回答

Paulo Silva
Paulo Silva 2011-6-22
%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)

更多回答(1 个)

Laura Proctor
Laura Proctor 2011-6-22
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 个评论
Muhammad Asad
Muhammad Asad 2011-6-22
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by