String in a Loop

조회 수: 3 (최근 30일)
DARLINGTON ETAJE
DARLINGTON ETAJE 2022년 8월 17일
편집: dpb 2022년 8월 18일
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

채택된 답변

dpb
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
DARLINGTON ETAJE
DARLINGTON ETAJE 2022년 8월 18일
Thanks dpb
The problem is that the code works perfectly on script but fails in the app designer
Here is the code on MATLAB script:
hgdawe='OneTwoThreeFourFiveSixSevenEightNine';
bbU=[1;4;7;12;16;20;23;28;33];
ccU=[3;6;11;15;19;22;27;32;36];
ddU=[bbU ccU];
Finals=([]);
for i=1:1:9
bbD=bbU(i);
ccD=ccU(i);
Groupings=hgdawe(bbD:ccD);
Finals=[Finals string(Groupings)];
end
Here is the same logic on MATLAB App Designer which fails
for iyy=1:1:nL
bbT=ssfT(iyy);
ccT=sscT(iyy);
In_Use_Legend=hgdawe(bbT:ccT);
Finalsz=[Finalsz string(In_Use_Legend)];
end
The error I get in the app designer is: Index exceeds the number of array elements. Index must not exceed 1.
What do I do?
dpb
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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by