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

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

 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

Genius!! Thank you for the prompt response. Where in the Matlab literature does it teach one this? What is @(x)?
In short, @ is a function handle (a bit like a pointer), it is a way to pass a function to another or to define functions inline.
>> mySin = @sin ;
>> mySin( pi/2 )
ans =
1
This is useful when you want to pass a function to an ODE solver for example. If you want to integrate the sine function, you have to pass the function SIN to the integrator, and one way to pass a function is through a handle:
[t,y] = ode45( @(t,y) sin(t), [0, pi], 0 ) ;
This was for handles. Now we can create a handle on a function that we don't name (anonymous). If you didn't know about function SIND (sine in degree) and wanted to convert the what ODE45 passes to the function it integrates from degree to radian, you could either write a function for this purpose (and create an M-File for that), or create an anonymous function inline, directly in the call to ODE45:
[t,y] = ode45( @(angle_deg, y) sin(angle_deg*pi/180), [0, 180], 0 ) ;
where @(angle_deg) defines a function with one argument named internally angle_deg and the body of the function is the expression that follows.
Finally, a handle is an object that can be saved in a variable, so you can name the functions that you create inline:
sinDegree = @(angle_deg, y) sin(angle_deg*pi/180) ;
[t,y] = ode45( sinDegree, [0, 180], 0 ) ;
class( sinDegree )
Charles, it sounds like Cedric solved your problem, so you can "Thank" him by clicking the "Accept this Answer" link and the "vote" link to give him reputation points.
Thank you for this. I now have what looks likes a structure array as output. Each cell of the structure array is n x 5. I wish to attract columns 2, 3,4 from each cell. However to be honest I merely wish to index into each cell, and use columns and assign each column to a variable. These variables will then be inputs to another script. How do I index into each cell and assign columns 2, 3,and 4 to variables D, E, and F?
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.

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Products

Tags

Asked:

on 8 Oct 2017

Commented:

on 8 Oct 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!