Matlab Array indixing error

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 Comments

Dyuman Joshi
Dyuman Joshi on 18 Nov 2022
Edited: Dyuman Joshi on 18 Nov 2022
What is the type of variable Nom and how many elements does it have?
Also, since the error occurs while running the code as a part of the function, I would suggest you to show the function code as well.
Nom = [1,2]; % two elements
Nom(end-1:end)
ans = 1×2
1 2
Nom = 3; % one element
Nom(end-1:end)
Array indices must be positive integers or logical values.
Have a look at the size of the array and the indices you are requesting.
%% analyse du nom d'une barre pour extraire l'info Tension
function [NomB,Tension,Flag]=NomBarreMdl(Nom)
% NOMBARRE analyse le nom d'une barre. Si une information Tension est présente dans
% le nom de la barre (c'est-à-dire si la dernière ligne du nom est un
% nombre terminé par v, V, kv ou kV, alors Flag est positionné à 1 et la fonction retourne la
% tension de la barre. On ajoute U= dans le nom de la barre pour pouvoir le détecter ultérieurement.
% dans le cas contraire, Flag vaut 0.
%
% Module créé lors de la ModifN14
x=strfind(Nom,sprintf('\n'));
% Recherche de l'unité kV dans le nom de la barre
y1=Nom(end-1:end); % THIS IS WHERE THE ERROR IS
if ~isempty(x)&&(min(y1=='kv')||min(y1=='kV')||min(y1=='KV')||min(y1=='Kv'))
Tension=1000*str2num(Nom(x(end)+1:end-2));
if isempty(Tension)
Flag=0;
NomB=Nom;
else
Flag=1;
NomB=[Nom(1:x-1) ' U=' num2str(Tension) 'V'];
end
else
NomB=Nom;
Tension=[];
Flag=0;
end
% Recherche de l'unité V dans le nom de la barre si kV non trouvé.
if Flag == 0
y=Nom(end);
if ~isempty(x)&&((y=='v')||(y=='V'))
Tension=str2num(Nom(x(end)+1:end-1));
if isempty(Tension)
Flag=0;
NomB=Nom;
else
Flag=1;
NomB=[Nom(1:x-1) ' U=' num2str(Tension) 'V'];
end
else
NomB=Nom;
Tension=[];
Flag=0;
end
end
the third line of the function is where the error is. It's supposed to give the result 'kV' with Nom = #TrAF115kV so i don't understand why
"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.
oh ok thx i will try that

Sign in to comment.

 Accepted Answer

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 Comments

"I suspect in your case, you have a variable on the name end."
Difficult to achieve, a simple assignment will not work:
end = 5;
Illegal use of reserved keyword "end".
Another possibility is that the variable in question has either 0 or 1 elements. In the case of a scalar end will be 1 and so the expression end-1:end is [0 1].
try
x = 42;
x(end-1:end)
catch ME
fprintf("This code threw the error '%s'", ME.message)
end
This code threw the error 'Array indices must be positive integers or logical values.'
In the case of an empty end is 0 and so the expression end-1:end is [-1 0].
try
x = [];
x(end-1:end)
catch ME
fprintf("This code threw the error '%s'", ME.message)
end
This code threw the error 'Array indices must be positive integers or logical values.'
@KSSV i don't have a variable on the name end.
@Steven Lord the variable y1 takes two last member of the arry Nom which is composed of #TrAF115kV so it should take the value kV.
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.
ok it was a string that's why it didn't work thx for the answer

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022a

Tags

Asked:

Ali
on 18 Nov 2022

Commented:

Ali
on 18 Nov 2022

Community Treasure Hunt

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

Start Hunting!