For Programmers: Free Programming Magazines  


Home > Archive > Matlab > January 2008 > passing variables to @function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author passing variables to @function
Johannes

2008-01-24, 8:57 am

Hi,

I've a gui with an uibuttoingroup and I want to invoke a
function when a radiobutton on the panel has been seleted.
Now what I've tried is to put the following in the create
function:

set(hObject,'SelectionChangeFcn',@select
ion_changed);

This works pretty well, but I want to pass some variables
along to the function. In the help I found something like

handle = @(arglist)anonymous_function

When I try to invoke the function like that

set(hObject,'SelectionChangeFcn',@(my_va
riable)
selection_changed);

and the function callback is like that

function selection_changed(source,eventdata, my_variable)

I get an error message. I also tried this:

function selection_changed(my_variable)

But I always get an error message. So how can I pass
variable along with the function?

Thanks for any help,
Johannes.



Anh Huy Phan

2008-01-24, 8:57 am

"Johannes " <RudiRocker@gmx.net> wrote in message
<fn9pbt$esb$1@fred.mathworks.com>...
> Hi,
>
> I've a gui with an uibuttoingroup and I want to invoke a
> function when a radiobutton on the panel has been seleted.
> Now what I've tried is to put the following in the create
> function:
>
> set(hObject,'SelectionChangeFcn',@select
ion_changed);
>
> This works pretty well, but I want to pass some variables
> along to the function. In the help I found something like
>
> handle = @(arglist)anonymous_function
>
> When I try to invoke the function like that
>
> set(hObject,'SelectionChangeFcn',@(my_va
riable)
> selection_changed);
>
> and the function callback is like that
>
> function selection_changed(source,eventdata, my_variable)
>
> I get an error message. I also tried this:
>
> function selection_changed(my_variable)
>
> But I always get an error message. So how can I pass
> variable along with the function?
>
> Thanks for any help,
> Johannes.
>
>
>


You can use this way

set(hObject,'SelectionChangeFcn',{@selec
tion_changed,var1,
var2});


And your function selection_changed is defined as follow

function selection_changed(varargin)

....

In this case, you will obtain

varargin{3} - var1
varargin{4} - var2


HTH

Anh Huy Phan
RIKEN - BSI



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com