can i used cellfun with a function which has more than one input?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
3 votes
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
Charles
on 8 Oct 2017
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 )
Image Analyst
on 8 Oct 2017
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.
Charles
on 8 Oct 2017
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).
Charles
on 8 Oct 2017
Yes. Correct the function returns a cell array. Thank you for this. Much appreciated. I will try it
Cedric
on 8 Oct 2017
My pleasure!
More Answers (0)
Categories
Find more on Function Creation in Help Center and File Exchange
Products
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)