February 10, 2014 0

Matlab: calling a function handle with parameters from a cell array

By in matlab

Having a function handle sometimes means that you want to be flexible regarding the number of input arguments.

having a function handle fhandle=@plot
means that you can call fhandle() but is you want to pass a variable number of arguments to fhandle that resides in a cell array, cparameters, containing e.g.: cparameters={[1:10],’resize’,’no’} you can do:
fhandle(cparameters{:}) which will result in e.g. plot([1:10],’resize’,’no’)

In the case where you want additional stuff to happen – e.g. add title to the plot you can do:

fhandle=@(data,titlestr,varargin) plot(data,varargin{:}) && title(titlestr)
cparameters={1:10,'test title','LineWidth',5}

fhandle(cparameters{:})

Will produce
plot(1:10,’LineWidth’,5) && title(titlestr)

Tags: , , ,


Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/mgis.dk/about/wp-includes/class-wp-comment-query.php on line 399

Leave a Reply