How to save a variable from a function and use it in another function in MATLAB GUI?

2 ビュー (過去 30 日間)
Hi,
I have made a MATLAB GUI which has two buttons. I want to perform some tasks on CallBack event of both these buttons.
I have obtained a variable FileName when the first button is pressed. But as soon as the function of first button ends, the variable gets deleted. I want to use this variable when the second button is pressed which is obviously a different function. Since, the first function variable is deleted as soon as the function ends, how can I use this variable within the second button's function.
I know how to do it by saving it as .mat and recalling it but it will make the GUI interface unfriendly. Can you please tell me another way to save a variable before the first function ends and recall it later in the second function?
Thank you.

採用された回答

Gerd
Gerd 2011 年 6 月 22 日
Hi Asad,
before the "first button" function end use
mydata.value = "directory result"
setappdata(gcf,'mydata',mydata);
At the beginning of the "second button" function use
mydata = getappdata(gcf, 'mydata');
disp(mydata.value); % shows the value
  2 件のコメント
Muhammad Asad
Muhammad Asad 2011 年 6 月 22 日
Actually the first button is the browse button. So, in the first button function, I have this code:
[FileName,PathName]=uigetfile
FullPath=[PathName FileName]
Now, I want to use this FullPath variable to be passed to second function. Can you tell me how the above code lines should be modified according to these variable names?
Muhammad Asad
Muhammad Asad 2011 年 6 月 22 日
Okay, I did it. Thanks :)

サインインしてコメントする。

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2011 年 6 月 22 日
  1 件のコメント
Krishna Kumar
Krishna Kumar 2011 年 6 月 22 日
Yes.I had suggested the same
in OP's thread in newsgroup-http://www.mathworks.com/matlabcentral/newsreader/view_thread/309582

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by