Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Passing input arguments to a function from a GUI

Subject: Passing input arguments to a function from a GUI

From: Martin

Date: 11 Feb, 2009 18:16:02

Message: 1 of 4

Hi all,

I have written a function which takes 10 input arguments and performs some computations. I would like to create a GUI for demonstration purposes where the user can vary each of the 10 input arguments (I have created pop up menus for this). I also want when the user selects the values for the inputs (and presses the push button initializing the simulation) these to be passed as input arguments to the function, which will make the necessary computations and will plot three of the outputs (predetermined by me).

I have created the handles structure in such a way so that I have a field for each pop up menu - for example handles.nbsim will take the value of the number of simulations that the user selects. I use the switch statement for each pop-up menu and assign all the possible choices as cases - e.g:

case '1' %user selects one simulation run
handles.nbsim=1;

and so on. Then I have the callback for the push button which will call back the main function which will be performing the computations and plotting. I wrote the fields of the handles structure as input arguments like so:

[outputs]=function(handle.T,handle.N,handle.nbsim....)

But i get errors. I tried copying the whole function body instead of just referring to the function and substitute handles.input parameter for all the corresponding parameters which have to be selected. I also get errors unrelated to this specific callback - for example:

Error in ==> gui_mainfcn at 96
        feval(varargin{:});

Error in ==> GUI1 at 42
    gui_mainfcn(gui_State, varargin{:});

Error in ==> @(hObject,eventdata)GUI1('pushbutton1_Callback',hObject,eventdata,guidata(hObject))

 
??? Error while evaluating uicontrol Callback

Can you help me out? What is the correct way to do this? All the examples I've come across first initialize with the necessary data to be ploted and then the user just chooses the type of plot. I need the user to provide values for the inputs and then to perform the computation and plot.

Sorry for the long post and thanks in advance.

Best wishes,
Martin
 

Subject: Passing input arguments to a function from a GUI

From: Matt Fig

Date: 12 Feb, 2009 01:37:02

Message: 2 of 4

"Martin " <martin_anastasov@yahoo.com> wrote in message Then I have the callback for the push button which will call back the main function which will be performing the computations and plotting. I wrote the fields of the handles structure as input arguments like so:
>
> [outputs]=function(handle.T,handle.N,handle.nbsim....)


You can't do that. Matlab won't parse a structure in an input argument. Pass in the structure and do the separations inside.

 
> But i get errors. I tried copying the whole function body instead of just referring to the function and substitute handles.input parameter for all the corresponding parameters which have to be selected. I also get errors unrelated to this specific callback - for example:
>
> Error in ==> gui_mainfcn at 96
> feval(varargin{:});
>
> Error in ==> GUI1 at 42
> gui_mainfcn(gui_State, varargin{:});
>
> Error in ==> @(hObject,eventdata)GUI1('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
>
>
> ??? Error while evaluating uicontrol Callback
>


It looks like you are having trouble understanding callbacks. When an object executes a callback, it always passes at least two things, first it's own handle, then a structure containing eventdata. Any other things you want passed to the function come in as the 3rd+ arguments. If you use varargin, then you need to unpack it that way.

callingobjectshandle = varargin{1};
firstneededargument = varargin{3};
secondneededargument = varargin{4};
etc.


Here is an example. In the GUI there is a function called ranger, which is a callback to a slider object. I have written it two ways, one to use varargin. Try them both out, one at a time and see if you understand it.

%-------------------------------------------------------------------------------%

function [] = gui_act
% Uses callback functions. Use a slider to control an axes.
x = 0:.1:300;
f = figure;
ax = axes;
plot(x,sin(x));
xlim([0,pi]);
ylim([-1,1])
hf1 = figure('uni','pix','menub','no','pos',[400 400 220 40]);
str = {'sty','sli','uni','pix','pos'};
sl = uicontrol(hf1,str{:},[10 10 200 20],'min',1,'val',pi,'max',300,...
     'call',{@ranger,ax},'deletefcn',{@delete,f});%#ok
 set(f,'deletef',{@delete,hf1})
 
function [] = ranger(h,evnt,ax) %#ok
% Callback for the slider. Uses individual inputs.
set(ax,'xlim',[0 get(h,'val')],'ylim',[-1,1]);

% function [] = ranger(varargin) %#ok
% % Callback for the slider. Uses varargin
% [h evnt ax] = deal(varargin{:});
% set(ax,'xlim',[0 get(h,'val')],'ylim',[-1,1]);

%-------------------------------------------------------------------------------%




YW[k_[jeeu[ue[edXCa6^OuWbWuW|olbd_YfcWWfj^W]|cu^jeueX0k;u$u

Subject: Passing input arguments to a function from a GUI

From: Occa O'Connor

Date: 12 Feb, 2009 07:53:04

Message: 3 of 4

G'Day Matt,

I was about to ask something similar because I'm having similar problems with trying to modify the java on a slider to instantanously update a text box. I really appreciate your examples above because I've spent the whole night trying to find the extra arguments that were getting passed which I couldn't find.

A further question is however, I'm trying to call a function thats internal to an m.file. The syntax for this (GUIDE produced .m file) is Mfile('function name', hObject, eventData, varargin....). So when setting your call back properties, how do you make it put the 'function name' as varargin(1) before the object-handle and event data.

The method I've tried and failed on is:

set(hjSlider, 'AdjustmentValueChangedCallback', {@(hObject, eventdata)MFileName('InternalFunctionName', hObject, eventdata, guidata(hObject))})

I've also tried just:

hjSlider.AdjustmentValueChangedCallback = @(hObject, eventdata)MFileName('InternalFunctionName', hObject, eventdata, guidata(hObject))

If I just do:
hjSlider.AdjustmentValueChangedCallback = {@MFileName, guidata(hObject)}
It passes the arguments as you've stated about and trys to run the whole m file from the start (recursive error comes up).

Thanks for the help once again

Cheers

Occa

Subject: Passing input arguments to a function from a GUI

From: Martin

Date: 12 Feb, 2009 10:09:03

Message: 4 of 4

"Matt Fig" <spamanon@yahoo.com> wrote in message <gmvufu$b0u$1@fred.mathworks.com>...
> "Martin " <martin_anastasov@yahoo.com> wrote in message Then I have the callback for the push button which will call back the main function which will be performing the computations and plotting. I wrote the fields of the handles structure as input arguments like so:
> >
> > [outputs]=function(handle.T,handle.N,handle.nbsim....)
>
>
> You can't do that. Matlab won't parse a structure in an input argument. Pass in the structure and do the separations inside.
>
>
> > But i get errors. I tried copying the whole function body instead of just referring to the function and substitute handles.input parameter for all the corresponding parameters which have to be selected. I also get errors unrelated to this specific callback - for example:
> >
> > Error in ==> gui_mainfcn at 96
> > feval(varargin{:});
> >
> > Error in ==> GUI1 at 42
> > gui_mainfcn(gui_State, varargin{:});
> >
> > Error in ==> @(hObject,eventdata)GUI1('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
> >
> >
> > ??? Error while evaluating uicontrol Callback
> >
>
>
> It looks like you are having trouble understanding callbacks. When an object executes a callback, it always passes at least two things, first it's own handle, then a structure containing eventdata. Any other things you want passed to the function come in as the 3rd+ arguments. If you use varargin, then you need to unpack it that way.
>
> callingobjectshandle = varargin{1};
> firstneededargument = varargin{3};
> secondneededargument = varargin{4};
> etc.
>
>
> Here is an example. In the GUI there is a function called ranger, which is a callback to a slider object. I have written it two ways, one to use varargin. Try them both out, one at a time and see if you understand it.
>
> %-------------------------------------------------------------------------------%
>
> function [] = gui_act
> % Uses callback functions. Use a slider to control an axes.
> x = 0:.1:300;
> f = figure;
> ax = axes;
> plot(x,sin(x));
> xlim([0,pi]);
> ylim([-1,1])
> hf1 = figure('uni','pix','menub','no','pos',[400 400 220 40]);
> str = {'sty','sli','uni','pix','pos'};
> sl = uicontrol(hf1,str{:},[10 10 200 20],'min',1,'val',pi,'max',300,...
> 'call',{@ranger,ax},'deletefcn',{@delete,f});%#ok
> set(f,'deletef',{@delete,hf1})
>
> function [] = ranger(h,evnt,ax) %#ok
> % Callback for the slider. Uses individual inputs.
> set(ax,'xlim',[0 get(h,'val')],'ylim',[-1,1]);
>
> % function [] = ranger(varargin) %#ok
> % % Callback for the slider. Uses varargin
> % [h evnt ax] = deal(varargin{:});
> % set(ax,'xlim',[0 get(h,'val')],'ylim',[-1,1]);
>
> %-------------------------------------------------------------------------------%
>
>
>
>
> YW[k_[jeeu[ue[edXCa6^OuWbWuW|olbd_YfcWWfj^W]|cu^jeueX0k;u$u

Thank you Matt Fig. I appreciate your contribution. Yes I didn't spend much time looking at callbacks because I just need the GUI for a presentation - the actual algorithm is much more important for me. I did something different and it worked. I created structures in the opening function to hold all the data that can be selected by the user and then for each menu again I use the switch statement to detect what the user selected and pass it to another structure. Then for the push button I pass these structures as arguments. Maybe it's more tedious this way but at least I got it to do what I want. Thanks again.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us