Index exceeds number of array elements (4).
이전 댓글 표시
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
Scott MacKenzie
2021년 6월 27일
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
2021년 6월 27일
Scott MacKenzie
2021년 6월 27일
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
2021년 6월 28일
답변 (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
indrani chatterjee
2021년 6월 28일
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
2021년 6월 28일
Cris LaPierre
2021년 6월 28일
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
2021년 6월 28일
Cris LaPierre
2021년 6월 28일
편집: Cris LaPierre
2021년 6월 28일
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
2021년 6월 28일
Cris LaPierre
2021년 6월 28일
And what happens when you comment out the function block?
indrani chatterjee
2021년 6월 28일
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



