Create a global variable in a GUI

Im making a gui which has a two buttons:
function button_browse_Callback(hObject, eventdata, handles)
filename = uigetfile
function button_load_Callback(hObject, eventdata, handles)
fid=fopen(filename,'rt');
....etc
but I cant do the above because button_load_Callback does not have access to filename.
how can I pass the filename from button_browse_callback to button_load_callback?

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 28 日
編集済み: Azzi Abdelmalek 2013 年 9 月 28 日

14 投票

You don't need global variables, use guidata
function button_browse_Callback(hObject, eventdata, handles)
filename = uigetfile
...
handles.filename=filename
guidata(hObject,handles)
%-------------------------------------------------------------------------------------------------------
function button_load_Callback(hObject, eventdata, handles)
filename=handles.filename
fid=fopen(filename,'rt');
...

4 件のコメント

Sai
Sai 2017 年 5 月 13 日
編集済み: Sai 2017 年 5 月 13 日
can I not use 'handles.filename' as a variable directly. Like:
function button_browse_Callback(hObject, eventdata, handles)
handles.filename = uigetfile
guidata(hObject,handles)
Image Analyst
Image Analyst 2017 年 5 月 13 日
You can as long as it's in a function that has access to your master handles structure (like an official callback function that GUIDE made) AND you either use guidata() inside an official callback function, OR you pass handles back out to an official callback that calls guidata in the case you're writing your own custom function.
Khine zar
Khine zar 2018 年 3 月 17 日
thank you !.... Your answer is helpful for me !
Miguel Reyes
Miguel Reyes 2018 年 8 月 24 日
Thanks! for your practical idea

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2013 年 9 月 28 日
編集済み: Image Analyst 2015 年 6 月 26 日

5 投票

Another way is to declare the variables global in every function that needs to access them.
function button_browse_Callback(hObject, eventdata, handles)
global baseFileName folder;
[baseFileName, folder] = uigetfile();
function button_load_Callback(hObject, eventdata, handles)
global baseFileName folder;
fullFileName = fullfile(folder, baseFileName);
fid=fopen(fullFileName ,'rt');
Any function that declares the variables baseFileName and folder as global will be able to see the variables. They can be created and changed in any function that has that global declaration line. Functions that do not have the global declaration line will not be able to see those variables.

6 件のコメント

Anshul Mittal
Anshul Mittal 2014 年 8 月 13 日
Thanks a lot! Exactly what I was looking for. Nice links too!
Ash
Ash 2015 年 4 月 22 日
Great
Dean Shanahan
Dean Shanahan 2015 年 6 月 26 日
Exactly what I was looking for. Thank you.
John
John 2015 年 9 月 21 日
Why do i have to declare the global variables at the beginning of eacht function that wants to use it? doesnt it contradict the idea of global variables...i am just facing a similar issue! i have several GUIs with several callbacks created with GUIDE that should have access to input data given from the user. Thats why i want to declare those input variables global!!
If i write
global Tolerance MaxIterations Numberoftimeinst
in the opening function of the Front Surface GUI. All other GUIs open when the user presses push Buttons on this main GUI. Nevertheless, although declaring the variables global, i cannot just set values or get values in other GUI:(
So do i really have to declare it every time again so that it works?
Best regards, John
Stephen23
Stephen23 2015 年 9 月 21 日
編集済み: Stephen23 2015 年 9 月 21 日
John
John 2015 年 9 月 21 日
Hi Stephen, i know used guidata for passing values between several GUIs but now i want to execute a complex function wihtin a GUI and this function doesnt have input parameters but works with globally defined variables. Thats why i would like to do it this easy way...i thought declaring variables as global in the opening function of the mainGUI would make them visible all over the main GUIs callbacks and also for the children of the parent mainGUI... I am wrong with that right? so i always have to declare them again vefore i have access....

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

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by