How to save 2 variables from function and use it in another function in MATLAB GUI?
显示 更早的评论
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.
采纳的回答
更多回答(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.
类别
在 帮助中心 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!