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);
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Classification Ensembles 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!