Why is subsref and subscripted reference not equivalent

조회 수: 1(최근 30일)
Reza Housseini
Reza Housseini 2017년 2월 21일
답변: Kenneth Eaton 2017년 2월 21일
Assume an struct array with the following content:
A = struct('b', {1, 2});
Following two code blocks do not give the same output:
ans =
1 2
S = struct('type', {'.'}, 'subs', {'b'});
[subsref(A, S)]
ans =
But I would say this two referencing methods are equal. What is the workaround for using subsref?
  댓글 수: 3
Alexandra Harkai
Alexandra Harkai 2017년 2월 21일
Thanks for the clarification!

댓글을 달려면 로그인하십시오.


Kenneth Eaton
Kenneth Eaton 2017년 2월 21일
Not sure if this is a complete answer, but it appears that invoking subsref directly requires a stricter definition of the output arguments. While:
will automatically dump all possible output arguments in a comma separated list, calling subsref directly requires you to explicitly define how many output arguments you want. The only way I've figured out thus far to get them all is to collect them in a cell array of the appropriate size:
>> [out{1:numel(A)}]=subsref(A, S)
out =
1×2 cell array
[1] [2]
>> [out{:}]
ans =
1 2

Jan 2017년 2월 21일
편집: Jan 2017년 2월 21일
Not an answer: I've played around with
S = struct('type', {'()', '.'}, 'subs', {{':'}, 'b'});
[subsref(A, S)]
is actually:
But this still replies 1 instead of the expected [1,2]. Then my interest in the question growed and I've voted for it. I expect, that there is a simple solution, by I cannot find it currently.


Find more on Startup and Shutdown 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!

Translated by