Matlab Array indixing error

2 ビュー (過去 30 日間)
Ali
Ali 2022 年 11 月 18 日
コメント済み: Ali 2022 年 11 月 18 日
Hi,
On Matlab 2022 i have the following error :
Array indices must be positive integers or logical values.
Error in mdl2List>NomBarreMdl (line 1022)
y1=Nom(end-1:end);
I don't know why it gives me this error because when i use the debugger and i pause on the same line and i use this line on the matlab command it works perfectly. But in the function it doesn't.
  5 件のコメント
Stephen23
Stephen23 2022 年 11 月 18 日
"so i don't understand why"
Your function assumes that NOM has two or more elements, but does not check this in any way. This is a bug.
Ali
Ali 2022 年 11 月 18 日
oh ok thx i will try that

サインインしてコメントする。

採用された回答

KSSV
KSSV 2022 年 11 月 18 日
In MATLAB the indices should be posittive integers or logicals. If not it will throw the error which you have shown.
% EXample
A = rand(1,10) ;l
A(1) % no error
A(end) % no error; gives last element
% logica lindexing
idx = A>0.5 ;
A(idx)
%
A(0.5) % error. Index cannot be fraction
A(0) % error index cannot be 0
So, I suspect in your case, you have a variable on the name end. Check
whos end
  5 件のコメント
Steven Lord
Steven Lord 2022 年 11 月 18 日
Is Nom a char vector or a string?
Nom_char = '#TrAF115kV'
Nom_char = '#TrAF115kV'
Nom_string = string(Nom_char)
Nom_string = "#TrAF115kV"
whos Nom_char Nom_string
Name Size Bytes Class Attributes Nom_char 1x10 20 char Nom_string 1x1 156 string
Nom_char has 10 characters and so asking for the last two characters works.
Nom_char(end-1:end)
ans = 'kV'
Nom_string is 1 string and so asking for the last two elements using indexing wouldn't work. I'll show that as the last line of code in this comment. You could retrieve the last two characters of either using extractAfter instead of indexing.
c1 = extractAfter(Nom_char, strlength(Nom_char)-2)
c1 = 'kV'
s1 = extractAfter(Nom_string, strlength(Nom_string)-2)
s1 = "kV"
Nom_string(end-1:end) % This doesn't work
Array indices must be positive integers or logical values.
Ali
Ali 2022 年 11 月 18 日
ok it was a string that's why it didn't work thx for the answer

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by