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:
create nested callback functions as needed

Subject: create nested callback functions as needed

From: Jeremy

Date: 4 May, 2010 18:30:23

Message: 1 of 5

Hello,

I'm working on creating a list of check boxes within a GUI (self-coding, not using GUIDE). My problem is that the number of checkboxes that need to be displayed is going to change depending on the number of columns in an Excel file that is being opened. For example, one Excel file may have 10 columns (correspond to 10 unique experimental datasets), while the next Excel file may have 12 columns.

It was trivial to display the variable number of check boxes, but now I'm wondering if there is a way to dynamically create callback functions within a loop, depending on the number of checkboxes. Alternately, is there a way to re-use one callback function for multiple check boxes?

Any input would be greatly appreciated.
Thanks,
Jeremy

Subject: create nested callback functions as needed

From: Matt Fig

Date: 4 May, 2010 18:42:08

Message: 2 of 5

You can have them all call the same callback, just use a SWICTH on the calling handle.


function [] = ch_cb(HC,E,HAND)

switch HC

case HAND.rd1
% do something
case HAND.rd2
% do something
etc,....
end

Subject: create nested callback functions as needed

From: Jan Simon

Date: 4 May, 2010 18:47:04

Message: 3 of 5

Dear Jeremy!

> Alternately, is there a way to re-use one callback function for multiple check boxes?

You can pass additional parameters to a callback:
  uicontrol('Style', 'Checkbox', 'Callback', {@myCB, 1});

  function myCB(ObjH, EventData, Index)
  switch Index
  case 1
     ... callback for checkbox 1
  <etc.>
  end

Good luck, Jan

Subject: create nested callback functions as needed

From: Walter Roberson

Date: 4 May, 2010 19:18:00

Message: 4 of 5

Jeremy wrote:

> I'm working on creating a list of check boxes within a GUI (self-coding,
> not using GUIDE). My problem is that the number of checkboxes that need
> to be displayed is going to change depending on the number of columns in
> an Excel file that is being opened. For example, one Excel file may have
> 10 columns (correspond to 10 unique experimental datasets), while the
> next Excel file may have 12 columns.

> It was trivial to display the variable number of check boxes, but now
> I'm wondering if there is a way to dynamically create callback functions
> within a loop, depending on the number of checkboxes.

Yes, any anonymous function defined with @ is defined dynamically at run time.
If you have it within a loop, each of the created functions will be different
functions.

 > Alternately, is
> there a way to re-use one callback function for multiple check boxes?

Yes, there is no problem with that. If you need to distinguish which box was
checked, then you can examine the first parameter passed into the callback,
commonly named src in the Matlab documentation. src will be the handle of the
control that was clicked on (unless some code "manually" triggers the callback
and deliberately passes in something else.) Alternately, the gcbo function
will return the handle of the object that the user interacted with -- which
will be the same as src except in those cases of "manual" callbacks.

Having gotten myself mired into mentioning "manual callbacks", which are
probably of no interest to you in the situation you describe, I will give a
brief example to illustrate the difference between src and gcbo. It will be
indirectly useful in helping you see how to code multiple check boxes:


Suppose I have an edit box and a listbox. The user can enter a value in the
edit box, and upon pressing return, the edit box callback activates and the
program takes an action based upon the value the user entered. If the user
selects an entry from the listbox, the program is to take that same action
based upon the value selected. The easiest way to do this and ensure that the
code for doing the action is always exactly the same (consistency as changes
are made) is to only code the action once, in the edit box callback, and to
have the listbox callback work by setting the String of the edit box and then
placing a control to the callback of the edit box:

eb = uicontrol('Style','edit', 'Position', wherever1, 'Callback', {@eb_callback});

lb = uicontrol('Style','list', 'Position', wherever2, 'String', whatever,
'Callback', {@lb_callback,eb});

function lb_callback(src, evt, ebh)
   curidx = get(src, 'Value');
   vallist = get(src, 'String');
   curval = vallist{curidx};
   set(ebh, 'String', curval); %bash the edit box value
   cb = get(ebh, 'Callback'); %find the edit callback
   cb{1}(ebh, evt, cb{2:end}); %call the callback "manually"
end


If you have followed this, you will see that when eb_callback is invoked, it
might be because the user entered something into the edit box, or it might be
because the user did something to the list box and the listbox code called the
editbox callback. In the first case (interacted with the edit box directly),
the src parameter will be the edit box handle, and gcbo will also be the edit
box handle because that's what the user interacted with. In the second case
(interacted with the list box), within eb_callback, the src would be the
handle of the edit box, but only because the lb_callback deliberately passed
that value in; the lb_callback could have passed in just about anything
instead. gcbo on the other hand, being a matlab internal routine, knows which
control was really interacted with, so in this second case, within
eb_callback, gcbo would be the handle of the list box.


It's a difference that only makes a difference if you code in indirect ways --
but it can sometimes be a useful difference.

Anyhow... going back to your question: notice from this example that the same
callback, eb_callback, was used multiple times. Notice too that in
lb_callback, the value of the parameter ebh is going to be whatever value eb
had at the time the callback parameter {@lb_callback,eb} was set in the program.

You can quickly get from this example to something like:

for K = 1:n
   uicontrol('Style','check','Position', whereever3(K,:), 'Callback',
{@cb_callback,K})
end

Then within cb_callback, the third parameter would directly tell you which box
number you were working with.

Subject: create nested callback functions as needed

From: Jeremy

Date: 7 May, 2010 23:56:04

Message: 5 of 5

Thank you all for your comments! I'll work on implementing them ASAP.

-Jeremy

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