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!