MATLAB: Create a global variable in a GUI

globalguiguideMATLAB

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?

Best Answer

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');
...