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
채택된 답변
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
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
My pleasure!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
제품
태그
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
