Behavior of 'subsref' for arrays of objects

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

Imagine a simple array of structures, say:

A = struct('x', {1 2 3}, 'y', {'a' 'b' 'c'});

Asking for a given property for all this array's elements will give something like:

>> A.x ans =      1 ans =      2 ans =      3

Now, if I explicitly call the subsref function directly on this array, it only retrieves the first element's property:

>> builtin('subsref', A, substruct('.', 'x')) ans =       1

Why? And is there a possibility to call explicitly another built-in method that will retrieve the property for all the array's elements?

回答1:

The subsref method can return it but not as a comma separated list the way you get it in the interpreter. It returns them as separate output arguments that means:

>> [a,b,c]=builtin('subsref', A(:), substruct('.', 'x')) a =      1 b =      2 c =      3

you can capture the output in a cell array if you like

>> [x{1:numel(A)}]=builtin('subsref', A(:), substruct('.', 'x')) x =      [1]    [2]    [3]


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!