MATLAB Answers

2

can i used cellfun with a function which has more than one input?

Asked by Charles on 8 Oct 2017
Latest activity Commented on by Cedric Wannaz
on 8 Oct 2017
Hello I have a function as follows
[ chart_datavortex ] = chart_funcv( 'AUD_USD', New_dataopenbidx, New_datahighbidx, New_datalowbidx,New_datax, New_datavol )
The function takes 6 inputs, however i wish to use CELLFUN so as to input an array of the first input. Thus I want to do something like the following
C = {'GBP_USD', 'GBP_AUD', 'USD_CAD'}
A = cellfun(chart_funcv,C)
This fails with error
Not enough input arguments.
Please advise

  0 Comments

Sign in to comment.

Tags

Products

1 Answer

Answer by Cedric Wannaz
on 8 Oct 2017
Edited by Cedric Wannaz
on 8 Oct 2017
 Accepted Answer

Yes, use an anonymous function (or define a function inline => name your anonymous function) to wrap the call to chart_funcv, and pass the wrapper to CELLFUN:
C = {'GBP_USD', 'GBP_AUD', 'USD_CAD'} ;
wrapper = @(x) chart_funcv( x, New_dataopenbidx, New_datahighbidx, New_datalowbidx,New_datax, New_datavol ) ;
A = cellfun( wrapper, C ) ;
If the output of the function is not scalar, set 'UniformOutput' to false in the call to CELLFUN:
A = cellfun( wrapper, C, 'UniformOutput', false ) ;
and A will be a cell array of (non-scalar) outputs.

  7 Comments

What does chart_funcv output? If it is a cell array, then the output of CELLFUN is a cell array of cell arrays (all with the same size), and you can e.g. concatenate them:
result = cellfun( wrapper, C, 'UniformOutput', false ) ;
result = vertcat( result{:} ) ;
where result{:} is a comma separated list (CSL), so this call to VERTCAT is equivalent to:
result = vertcat( result{1}, result{2}, ... )
but works without you having to hard-code the whole thing with the correct number of cell contents.
Once concatenated, you can index e.g. column 2 with result(:,2). Note the block indexing (with parentheses). Block indexing a cell array returns the sub-cell array (block) indexed, which is a cell array as well. This is different from e.g. indexing result{1,3}, where curly-bracket indexing means: return the content of cell result(1,3).
Yes. Correct the function returns a cell array. Thank you for this. Much appreciated. I will try it

Sign in to comment.