Create a global variable in a GUI
51 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
Azzi Abdelmalek
2013 年 9 月 28 日
編集済み: Azzi Abdelmalek
2013 年 9 月 28 日
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 件のコメント
その他の回答 (1 件)
Image Analyst
2013 年 9 月 28 日
編集済み: Image Analyst
2015 年 6 月 26 日
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 件のコメント
Stephen23
2015 年 9 月 21 日
編集済み: Stephen23
2015 年 9 月 21 日
@John: you don't need to use globals, there are other neater and more robust tools available, as the first answer showed already: Use guidata instead.
- The first answer: "You don't need global variables, use guidata"
- http://matlab.wikia.com/wiki/FAQ#Are_global_variables_bad.3F
- http://de.mathworks.com/matlabcentral/answers/51946-systematic-do-not-use-global-don-t-use-eval
- http://blogs.mathworks.com/videos/2010/03/08/top-10-matlab-code-practices-that-make-me-cry/
- http://www.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html
- http://www.mathworks.com/help/matlab/matlab_prog/share-data-between-workspaces.html
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....
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!