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.
채택된 답변
추가 답변 (1개)
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.
카테고리
도움말 센터 및 File Exchange에서 Customize Object Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!