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.
0 件のコメント
採用された回答
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
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.
参考
カテゴリ
Help Center および File 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!