How to resolve subsref error when indexing with char subs?

Hi everyone,
I am trying to use subsref to get values out of an array. Let us say that I have a matrix,
a = rand(10,10);
While the following commands work as expected,
subsref(a,substruct('()',{1:2,10}))
subsref(a,substruct('()',{1:2,1:10}))
could someone help me figure out why
subsref(a,substruct('()',{1:2,'10'}))
subsref(a,substruct('()',{1:2,'end'}))
subsref(a,substruct('()',{1:2,'1:end'}))
do not work? The error returned in all cases is "Index exceeds matrix dimensions". Thanks a lot.

 채택된 답변

Walter Roberson
Walter Roberson 2017년 3월 12일

2 개 추천

Poking around it appears that end is not actually handled that way. end is a method that returns a value that is used to create the appropriate subsref structure. See https://www.mathworks.com/matlabcentral/newsreader/view_thread/19174 and https://www.mathworks.com/matlabcentral/cody/problems/2159-a-subsref-variant-that-accepts-the-end-operator
My experiments suggest that when : is used by itself as an index, that it is passed as ':' but that when a numeric range such as 1:10 is used, that expands to the numeric vector not to '1:10'. The situation like 2:end involves calling the end() method to find a numeric value that is then dropped into the colon operator to create a numeric range that is used in the subsref structure.

댓글 수: 1

Thanks, Walter, I had been converging on the same explanation. It does seem like the official documentation has got it wrong there.

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

추가 답변 (1개)

Steven Lord
Steven Lord 2017년 3월 11일
For this simple type of operation, you don't need to use subsref directly. Just index into the array using parentheses.
A = rand(10);
A(1:2, 10)
But since I'm guessing you're curious why the last three operations don't work, when you type:
A(1:2, '10')
you are attempting to get rows 1 and 2 and columns double('1') and double('0'). Since double returns the ASCII values for a character and '10' are characters 49 and 48 you're asking for columns 49 and 48 of a matrix with 10 columns.

댓글 수: 1

Thanks, Steven. This does answer part of my question. I am left wondering if it is at all possible to index into "end" manually using subsref, i.e., if there is a version of the following that works:
subsref(A,substruct('()',{1:2,'end'}))
I was under the impression that this could be done because I read so in the official documentation:
https://www.mathworks.com/help/matlab/matlab_oop/code-patterns-for-subsref-and-subsasgn-methods.html
Values of the Indexing Structure
When executing an indexing expression, MATLAB calls the class subsref or subsasgn method, if the class overloads these functions. One of the arguments passed to the method is the indexing structure. The indexing structure has two fields:
type — One of the three possible indexing types: '.', '()', '{}'
subs — A char vector with the property name or cell array of the indices used in the expression, including : and end.

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

카테고리

도움말 센터File Exchange에서 Customize Object Indexing에 대해 자세히 알아보기

태그

질문:

DT
2017년 3월 11일

댓글:

DT
2017년 3월 12일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by