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:
Callback

Subject: Callback

From: machatsk

Date: 20 Aug, 2010 16:00:23

Message: 1 of 7

Hello all,
   I have a gui that creates a new figure with a pushbutton when the user presses a button on the main gui figure. How do I create the callback and then do something with it in my code? I've tried...
             Thanks
             Maxx
uicontrol(.....'callback', {@process_push})

%Then to utilize the callback
function process_push

I get the error that there are too many input arguments...
 

Subject: Callback

From: Walter Roberson

Date: 20 Aug, 2010 16:04:39

Message: 2 of 7

On 20/08/10 11:00 AM, machatsk wrote:
> Hello all,
> I have a gui that creates a new figure with a pushbutton when the user
> presses a button on the main gui figure. How do I create the callback
> and then do something with it in my code? I've tried...
> Thanks Maxx
> uicontrol(.....'callback', {@process_push})
>
> %Then to utilize the callback
> function process_push
>
> I get the error that there are too many input arguments...

The Getting Started documentation about using callbacks is pretty good.
It would, in particular, tell you that every callback is automatically
passed two parameters, one reflecting the object that the callback is
with respect to, and the other containing some details of the callback.
The problem you had is that you did not allow for those two parameters

function process_push(src, evt)

Subject: Callback

From: machatsk

Date: 20 Aug, 2010 16:23:22

Message: 3 of 7

Walter Roberson <roberson@hushmail.com>

Wow. Easy. Would this be the same for getting the value of a checkbox in the same figure?

Subject: Callback

From: machatsk

Date: 20 Aug, 2010 16:43:04

Message: 4 of 7

"machatsk " <chatskom@chemimage.com>

> Wow. Easy. Would this be the same for getting the value of a checkbox in the same figure?

How would I name the checkbox so I could get the value when the process_push is pressed?
  
     get(NAME,'value')

Subject: Callback

From: Walter Roberson

Date: 20 Aug, 2010 18:23:58

Message: 5 of 7

On 10-08-20 11:23 AM, machatsk wrote:
> Walter Roberson <roberson@hushmail.com>
>
> Wow. Easy. Would this be the same for getting the value of a checkbox in
> the same figure?

Yes.

Subject: Callback

From: Frédéric Bergeron

Date: 20 Aug, 2010 18:29:05

Message: 6 of 7

"machatsk " <chatskom@chemimage.com> wrote in message <i4mbao$h09$1@fred.mathworks.com>...
> "machatsk " <chatskom@chemimage.com>
>
> > Wow. Easy. Would this be the same for getting the value of a checkbox in the same figure?
>
> How would I name the checkbox so I could get the value when the process_push is pressed?
>
> get(NAME,'value')

Hey,

if you use nested functions (see documentation), the name of the checkbox will be automatically passed to the callback function.
i.e.

function main_gui
figure;
handles.checkbox=uicontrol(...,'style','checkbox','callback',@checkbox_callback);

function checkbox_callback
value=get(handles.checkbox,'value');
disp(value);
end

end

Fred

Subject: Callback

From: Walter Roberson

Date: 20 Aug, 2010 18:30:36

Message: 7 of 7

On 10-08-20 11:43 AM, machatsk wrote:
> "machatsk " <chatskom@chemimage.com>
>
>> Wow. Easy. Would this be the same for getting the value of a checkbox
>> in the same figure?
>
> How would I name the checkbox so I could get the value when the
> process_push is pressed?
>
> get(NAME,'value')

With any name that is a legal Matlab variable -- e.g., must start with a
letter, and otherwise may contain digits, A-Z, a-z, and underscore ('_').


The tough part isn't naming the variable: the tough part is making sure that
the variable is in scope and has been given a value by the time you want to
retrieve the value.

I suggest you go into the Matlab documentation program and type in the key phrase

keeping variables in scope

as it shows an example there.

Tags for 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