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.
1 view (last 30 days)
Show older comments
zdot = calc_zdot(road_x, road_z, 10);
0 Comments
Answers (1)
Walter Roberson
on 25 May 2015
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.
0 Comments
See Also
Categories
Find more on Classification Ensembles in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!