How to pass variables from main script into GUI Callback function?

I used guide in order to create a graphic user interface. Now I have a pushbutton which has a Callback function.
Does anyone know how can I pass variables d and time_vector from the main script into the Callback function? You can find the piece of code below:
struct_G=guidata(Mygui); %I wanted to take out the Callback property of the created GUI
PushButton=struct_G.pushbutton1;
Here is the Pushbutton callback function:
pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
cla;
popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
case 1
plot(time_vector,d); %These are variables from the main script
end

8 Kommentare

Is it your goal to dynamically load those variables from the base workspace? Or are they parameters that can be loaded when starting your GUI?
You can pass them into the GUI as input arguments when you create it, or, if possible only calculate them within the GUI in the first place so they are already in there.
If you pass them in you can store them on the handles structure, e.g.
handles.time_vector = varargin{1};
handles.d = varargin{2};
then use the fields from handles in your callback.
@Adam, this can be tricky advice, as GUIDE generally creates callbacks in this form:
Mygui('pushbutton1_Callback',gcbo,[],guidata(gcbo))
So messing with the inputs will break all callbacks.
And people wonder why I'm generally oposed to GUIDE...
@Rik
They can be loaded when GUI runs. I am a bit lost in passing them through the callback function. Now I am trying to use code instead of GUIDE.
Another question: @(src,evt) has any specific meaning? I mean we should always use src,evt when we do the Callback? or they depend on the inputs of Callback?
The only problem I have ever come across passing arguments to a GUIDE UI when you launch it is that the first argument cannot be a string because this gets interpreted as the name of the program it is trying to launch. Apart from that silly design passing arguments in works fine.
Adam
Adam am 15 Jul. 2019
Bearbeitet: Adam am 15 Jul. 2019
All callbacks take those two arguments, src and evt. You don't have to call them that, you don't have to use them, but your callback needs to support them unless you squash them at creation time, e.g.
f = @(src,evt) someFunction( )
will not pass src and evt to someFunction when it runs.
src and evt are generally called hObject and eventdata in GUIDE.
My small guide to avoid GUIDE:
  • Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
  • Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
  • When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
  • You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
  • Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
  • More information about callbacks can be found in multiple places in the doc, for example here.
Thank you very much for the comprehend explaination!

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 15 Jul. 2019

Bearbeitet:

am 15 Jul. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by