Subsref Behavior for Object Arrays

Imagine a simple array of structures, for example:

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

A request for a given property for all of these array elements will yield the following:

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

Now, if I explicitly call the subsref function directly in this array, it only retrieves the first property of the element:

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

Why? Is it possible to call an explicitly different built-in method that will retrieve the property for all elements of the array?

+4
source share
1 answer

A method subsrefcan return it, but not as a comma-separated list , as you get in the interpreter. It returns them as separate output arguments, which means:

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

you can write the output to an array of cells if you like

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

All Articles