How to resolve subsref error when indexing with char subs?
Show older comments
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.
Accepted Answer
More Answers (1)
Steven Lord
on 11 Mar 2017
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 Comment
DT
on 11 Mar 2017
Categories
Find more on Customize Object Indexing 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!