How to use variables in between GUI function?

I am very new to GUI and so far for my understanding is that you can only put a tag's name behind handles. to communicate between different functions. For example, one uses axes(handles.axes1) inside a push button function.
For my case, I have several functions inside a GUI and there is one variable that is produced by one function, which is not defined previously when I made the GUI layout by GUIDE, it is only produced when the code is run and I would like to use it later in a pushbutton function, in this case, what should I do to make this variable outside of its mother function in order to be used in my pushbutton function?
Sorry if I sound confusing.
Any discussion/help is welcomed! Thanks!

 Accepted Answer

your_variable=10
handles.your_variable=your_variable
guidata(hObject,handles)
To call the variable
your_variable=handles.your_variable

8 Comments

This is the code I tried
%something
set(handles.file,'String','1.xls');
guidata(hObject, handles);
function Pushbutton1_Callback(hObject, eventdata, handles)
filecall = handles.file
winopen(filecall)
But it returns me errors of such
Reference to non-existent field 'file'.
Error in exercise>exercise_OpeningFcn (line 92)
set(handles.file,'String','1.xls');
%change this line
Error in gui_mainfcn (line 221)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [],
guidata(gui_hFigure), varargin{:});
Error in exercise(line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)exercise('Pushbutton_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Wait! So I tried
file='1.xls';
handles.filecall=file
guidata(hObject, handles);
instead of
set(handles.file,'String','1.xls');
guidata(hObject, handles);
And it worked fabulously! Thank you Azzi! By the way, do you mind explaining to me why this made such a difference?
Presumably handles.file is not a GUI handle. You can't use the 'set' syntax to create a field on your struct. If you want handles.file to just be a variable then use
handles.file = '1.xls';
Thanks Adam! So by GUI handle you mean the tags that were created from the CreateFcn? And since this is does not belong to the GUI layout, we cannot set it but what about handles.filecall? Doesn't this presume that filecall is a handle?
Adam
Adam on 26 Jul 2016
Edited: Adam on 26 Jul 2016
handles is just a simple struct, it is nothing magic. The fields that get added to it during creation of the GUI are the GUI components, these are graphics objects, each of which support the set syntax (though now since R2014b they also support the dot based class syntax) and these are the things that will have callbacks attached and which you interact with in those callbacks using your tags.
However, because handles is just a struct, you can add any field you want to it just like any other struct. These fields can be whatever you want - new graphics objects that weren't defined in GUIDE, cell arrays, user classes, simple scalar doubles, etc.
The 'magic' comes from the guidata( hObject, handles ) line which saves this handles struct back into your GUI so that next time you trigger a callback this version of the handles struct is passed to it rather than the one that was present immediately after creation of the GUI. If you miss this line out then your changes were just made to a local copy of the handles structure which goes out of scope when the current function ends.
I think I understands what you mean better now, so handles is a struct that you can add any field by using handles.tag/variable_name=something. You can only use set for an existing handle. And its best to use guidata(hObject, handles) to end your OpeningFcn and all your CallbackFcn that have handles defined in. My other question is, if you create your own function within GUI that generated new handles, do you need to end this function with guidata(hObject, handles) as well?
Thanks Adam, I am a newbie with GUI and your comments explain a lot.
Anything that changes non-graphics objects data on the handles struct will need a call to guidata after it in order to save it back into the main GUI.
If you have factored out a function from a callback or are using listeners you may not have 'hObject' though. In this case you can use gcbo instead or pass the handle of the main GUI down to your function and use
handles = guidata( hGUI );
to extract the handles struct, hGUI being the handle to your main GUI.
This makes sense, thanks for the tips! I will keep them in mind.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!