String in a Loop
1 回表示 (過去 30 日間)
古いコメントを表示
Hi Everyone,
Please help me out. I am trying to get the loop to work but am getting errors: Index exceeds the number of array elements. Index must not exceed 1. The In_Use_Legend is not working...
hgdawe='OneTwoThreeFourFiveSixSevenEightNine';
bbT=[1;4;7;12;16;20;23;28;33];
ccT=[3;6;11;15;19;22;27;32;36]
LL=size(ccT,1);
for i=1:1:LL
In_Use_Legend=hgdawe(bbT:ccT);
plot(app.UIAxes2,In_Use_VertSec,In_Use_TVD,'DisplayName',In_Use_Legend);
end
0 件のコメント
採用された回答
dpb
2022 年 8 月 17 日
In
In_Use_Legend=hgdawe(bbT:ccT);
you didn't subscript your lookup array variables but refer to the arrays each in their entirety.
To make the above work would be
In_Use_Legend=hgdawe(bbT(i):ccT(i));
But, this is the wrong way to do something of the sort; as you're discovering char() arrays are arrays and need both indices and this is inconvenient at best.
Use cellstr() or the new(ish) string class here instead --
lgndName={'One','Two','Three','Four','Five','Six','Seven','Eight','Nine'}; % cellstr array
for i=1:numel(hgdawe)
plot(app.UIAxes2,In_Use_VertSec,In_Use_TVD,'DisplayName',lgndName(i));
end
3 件のコメント
dpb
2022 年 8 月 18 日
編集済み: dpb
2022 年 8 月 18 日
Show us the whole thing and the error message in context -- in isolation we can't tell what it's complaining about -- and there are too many pieces undefined in the posted code snippet to guess.
But again, using a cellstr() insthead of char() array here is by far the better coding choice.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!