Index exceeds number of array elements (4).
Mostrar comentarios más antiguos
Hello, I am trying to perform active battery equalisation by using flyback topology. However, when ever I use more than 4 batteries, I get the error "Index exceeds number of array elements (4)".
How do I resolve this issue?
In the code, pwm(n) is a pulse given to the battery through a switch. soc(n) is the state of charge of a battery. s represents a stop signal when all state of charge is equal.
function [pwm1, pwm2, pwm3, pwm4, pwm5, s] = fcn(soc1, soc2, soc3, soc4, soc5)
pwm1 = 0;
pwm2 = 0;
pwm3 = 0;
pwm4 = 0;
pwm5 = 0;
if (soc1 > soc2 || soc1 > soc3 || soc1 > soc4 || soc1 > soc5)
pwm1 = dutycycle(50, 1/47000);
end
if (soc2 > soc1 || soc2 > soc3 || soc2 > soc4 || soc2 > soc5)
pwm2 = dutycycle(50, 1/47000);
end
if (soc3 > soc1 || soc3 > soc2 || soc3 > soc4 || soc3 > soc5)
pwm3 = dutycycle(50, 1/47000);
end
if (soc4 > soc1 || soc4 > soc2 || soc4 > soc3 || soc4 > soc5)
pwm4 = dutycycle(50, 1/47000);
end
if (soc5 > soc1 || soc5 > soc2 || soc5 > soc3 || soc5 > soc4)
pwm5 = dutycycle(50, 1/47000);
end
if (soc1 == soc2 && soc1 == soc3 && soc1 == soc4 && soc1 == soc5)
s = 1;
else
s = 0;
end
end
4 comentarios
Scott MacKenzie
el 27 de Jun. de 2021
There are no vectors in your function. The error must be originating outside the function in the rest of your code somewhere. More details would help.
indrani chatterjee
el 27 de Jun. de 2021
Scott MacKenzie
el 27 de Jun. de 2021
Hmm, well I don't know then. I don't use this toolbox. Does the error message give a line number or a line of code?
What about the dutycycle function? Isn't the first argunt supposed to be a vector? You're passing in a scalar.
indrani chatterjee
el 28 de Jun. de 2021
Respuestas (1)
The error is not coming from this function. You don't index anything. Somewhere else in your code you are trying to extract an element of your array that doesn't exist.
a = 1:4;
% works
a(4)
% doesn't work
a(5)
9 comentarios
indrani chatterjee
el 28 de Jun. de 2021
Cris LaPierre
el 28 de Jun. de 2021
Editada: Cris LaPierre
el 28 de Jun. de 2021
Perhaps you have overwritten the dutycycle function accidentally? Type the following command and share the results:
which dutycycle
On my computer, the result is "C:\Program Files\MATLAB\R2021a\toolbox\signal\signal\dutycycle.m".
Your function returns a result and no error if I pass in made-up values.
soc1 = 1;
soc2 = 2;
soc3 = 3;
soc4 = 4;
soc5 = 5;
[pwm1, pwm2, pwm3, pwm4, pwm5, s] = fcn(soc1, soc2, soc3, soc4, soc5)
% your function
function [pwm1, pwm2, pwm3, pwm4, pwm5, s] = fcn(soc1, soc2, soc3, soc4, soc5)
pwm1 = 0;
pwm2 = 0;
pwm3 = 0;
pwm4 = 0;
pwm5 = 0;
if (soc1 > soc2 || soc1 > soc3 || soc1 > soc4 || soc1 > soc5)
pwm1 = dutycycle(50, 1/47000);
end
if (soc2 > soc1 || soc2 > soc3 || soc2 > soc4 || soc2 > soc5)
pwm2 = dutycycle(50, 1/47000);
end
if (soc3 > soc1 || soc3 > soc2 || soc3 > soc4 || soc3 > soc5)
pwm3 = dutycycle(50, 1/47000);
end
if (soc4 > soc1 || soc4 > soc2 || soc4 > soc3 || soc4 > soc5)
pwm4 = dutycycle(50, 1/47000);
end
if (soc5 > soc1 || soc5 > soc2 || soc5 > soc3 || soc5 > soc4)
pwm5 = dutycycle(50, 1/47000);
end
if (soc1 == soc2 && soc1 == soc3 && soc1 == soc4 && soc1 == soc5)
s = 1;
else
s = 0;
end
end
indrani chatterjee
el 28 de Jun. de 2021
Cris LaPierre
el 28 de Jun. de 2021
I just noticed that this is in relation to a simulink model. Can you please share a screenshot with the error message showing?
indrani chatterjee
el 28 de Jun. de 2021
Cris LaPierre
el 28 de Jun. de 2021
Editada: Cris LaPierre
el 28 de Jun. de 2021
The error does not appear to be with your MATLAB Function block (my best guess). If it were, I believe the info under the error message would be "Component: MATLAB Function | Category: Coder error". Try commenting out the MATLAB Function block and see if you still get the error.
Also, is that the full error message? I feel like there should be more that what is shown. If there is, please copy/paste all of the text.
indrani chatterjee
el 28 de Jun. de 2021
Cris LaPierre
el 28 de Jun. de 2021
And what happens when you comment out the function block?
indrani chatterjee
el 28 de Jun. de 2021
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



