问题
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]
来源:https://stackoverflow.com/questions/30236807/behavior-of-subsref-for-arrays-of-objects