필터 지우기
필터 지우기

If I use a function calc_zdot which return a structure with two fields zdot.t and zdot.u. Can I pass the fields of the structure (t, u) to another function without mention zdot.t and zdot.u.

조회 수: 2 (최근 30일)
zdot = calc_zdot(road_x, road_z, 10);

답변 (1개)

Walter Roberson
Walter Roberson 2015년 5월 25일
Assigning to a variable and naming the fields is the easiest approach.
If you have a function that returns a structure and you want to break apart the structure and pass the parts as individual parameters to another call, then you can get as far as
struct2cell(calc_zdot(road_x, road_z, 10))
which would return a cell array containing the elements. It is possible to add a subsref() layer that would extract any one given member of the resulting cell array by position. It is also possible to get as far as
[first, second] = subsref(struct2cell(calc_zdot(road_x, road_z, 10)), struct('type', '{}', 'subs', {{':'}}));
and that would assign the elements to individual variables. But it is not possible to write an expression that expands to multiple arguments in a parameter list. But you could do this:
zhelper = @(z) SomeFunctionHere(some_argument1, some_argument2, z{:});
and then you could use
p = zhelper(struct2cell(calc_zdot(road_x, road_z, 10)));
to have the effect of calling SomeFunctionHere(some_argument1, some_argument2, z.t, z.u) where z stands for the structure returned by calc_zdot.
But if you are going to go through the trouble of introducing a helper function then you might as well just use
zhelper = @(z) SomeFunctionHere(some_argument1, some_argument2, z.t, z.u);
then
p = zhelper(calc_zdot(road_x, road_z, 10));
That kind of thing does have a role in building complicated anonymous functions, but most of the time, it is easier just to assign the results to a variable and then reference the fields of the variable.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by