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 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!