Ben Fossen
Ben Fossen

Reputation: 1007

Using varargin when passing argument into a function in Matlab?

I have a function called viewcsi(varargin) and I want to pass in three variables at most. The first is a MBSspectrum class I made and then a string and also a number.

viewcsi is a call back, it gets called like this:

...'ButtonDownFcn','viewcsi(''pickvox_cb'', sp_viewcsi)');

sp_viewcsi is the MBSspectrum class I made and is in the workspace. I want to be able to add another argument called counter which is integer of type double.

I want to do something like this:

...'ButtonDownFcn','viewcsi(''pickvox_cb'', sp_viewcsi, counter)');

or

...'ButtonDownFcn', {@viewcsi, 'pickvox_cb', 'sp_viewcsi', counter)');

But when I do the last two thing these do not work since they do not preserve 'sp_viewcsi' as a class but treats it like a string. What can I do to fix this? I have a feeling its something easy but I havent been able to figure it out.

Upvotes: 0

Views: 328

Answers (1)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38032

The ButtonDownFcn will only ever pass it two arguments. You can cheat it by saying

...'ButtonDownFcn',@(a,b)viewcsi(a,b, counter));

so that the callback will pass it a and b, while Matlab will hand it the current value of counter.

See also the doc on passing extra parameters.

Upvotes: 1

Related Questions