Output argument 'x' is not assigned on some execution paths.

Hello, I wrote the following code in the Matlab function in simulink and the error "Outut argument 'x' is not assigned on some execution paths" occurred. I don't know why the code not working. I hope you can help me. Thank you
My simulink function block have this code:
function [x,y] = fcn(u)
% u = battery voltage
% x=1 battery charge switch
% y=1 battery discharge switch
if (u<=0.9) % battery charge
while (u<=1.2)
x=1;
y=0;
end
elseif (u>1.2) || (u>0.9) && (u<1.2) %battery discharge
while (u>=0.9)
x=0;
y=1;
end
else
x=0;
y=0;
end
end

 Accepted Answer

Ok. I suspect you are trying to run this code on Simulink in the MATLAB function block. Since you didn't provide this information, so it is difficult to see what the issue is. Simulink use C/C++ code-generation and then compile your model. Therefore, it is necessary to initialize your variables, even if they will get their values later in code execution. Therefore, at the beginning of your function, initialize x, and y
function [x,y] = fcn(u)
% u = battery voltage
% x=1 battery charge switch
% y=1 battery discharge switch
x = 0; % this initialization is necessary for Simulink. Even if this is not goint to be used
y = 0;
% rest of your code
Also, use 'if', instead of while as pointed out by KSSV.

7 Comments

Yes,the problem was solved.
but,
if (u <= 0.9)% battery charging
if (u <= 1.2)
x = 1;
y = 0;
end
elseif
this condition not working. 1. if is working, 2. if is not working. u initial value <=0.9 , when u is greater than 0.9, y = 1 x = 0.
Can you describe your condition in words or write a mathematical equation of what you want to implement here. It seems like there will be an easier way, but I am not able to understand what you are trying to do in these lines.
If the battery voltage is less than 0.9, it will charge until it reaches 1.2 volts and exit this cycle.
If the battery voltage is between 0.9 and 1.2, it will discharge to 0.9 and return to the initial state by exiting this cycle.
So, for this purpose, the battery will not stay on continuous charge.
x=1 y=0 charge
y=1 x=0 discharge
By default, the MATLAB function block does not remember what happened in the previous time-step, so it will not remember whether it needs to charge or discharge of voltage is between 0.9 and 1.2. In that case, we need to use a persistent variable. Try the following code
function [x,y,z] = fcn(u)
% u = battery voltage
% x=1 battery charge switch
% y=1 battery discharge switch
x = 0;
y = 0;
persistent state % state=0<=>charging, state=1<=>discharging
if isempty(state)
state = 0;
end
if u <= 0.9
state = 0;
elseif u >= 1.2
state = 1;
end
if state==0
x = 1;
y = 0;
else
x = 0;
y = 1;
end
z = state;
end
I didn't know about this problem in Matlab. Thank you. My problem is solved.
Good work..
Thank you. I have the same problem. Your answer was very useful and solved my problem
guys can u help in coding for active cell balancing
this is my code
function [s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14] = fcn(soc1,soc2,soc3,soc4,soc5,soc6,soc7,pwm1,pwm2)
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s7=0;s8=0;
s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
high= max([soc1 soc2 soc3 soc4 soc5 soc6 soc7]);
low = min([soc1 soc2 soc3 soc4 soc5 soc6 soc7]);
if high==soc1&&low==soc2
s1=pwm1;s2=1;s5=pwm2;
s3=0;s4=0;s6=0;s7=0;s8=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc1&&low==soc3
s1=pwm1;s2=pwm1;s4= pwm2;s7=pwm2;
s3=0;s5=0;s6=0;s8=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc1&&low==soc4
s1=pwm1;s2=pwm1;s6=pwm2;
s3=0;s4=0;s5=0;s7=0;s8=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc1&&low==soc5
s1=pwm1; s4=pwm1; s7= pwm2;s11=pwm2;
s2=0;s3=0;s5=0;s6=0;s8=0;s9=0;s10=0;s12=0;s13=0;s14=0;
elseif high==soc1&&low==soc6
s1=pwm1;s7=pwm1;s10=pwm2;s12=pwm2;
s2=0;s3=0;s4=0;s5=0;s6=0;s8=0;s9=0;s11=0;s13=0;s14=0;
% soc2 starts here
elseif high==soc2&&low==soc1
s3=1;s4=pwm1;
s1=0;s2=0;s5=0;s6=0;s7=0;s8=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc2&&low==soc3
s3=pwm1;s5=pwm1;
s1=0;s2=0;s6=0;s7=0;s8=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc2&&low==soc4
s3=pwm1;s4=pwm1;s6=pwm2;
s1=0;s2=0;s5=0;s7=0;s8=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
%soc3 starts here
elseif high==soc3&&low==soc1
s3=pwm2;s5=pwm1;s6=pwm1;
s1=0;s2=0;s4=0;s7=0;s8=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc3&&low==soc2
s2=pwm1;s6=pwm2;s5=1;
s1=0;s3=0;s4=0;s7=0;s8=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc3&&low==soc4
s5=pwm1;s6=1;
s1=0;s2=0;s3=0;s4=0;s7=0;s8=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc3&&low==soc5
s5=pwm1;s7=pwm1;s9=pwm2;
s1=0;s2=0;s3=0;s4=0;s6=0;s8=0;s10=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc3&&low==soc6
s5=pwm1;s7=pwm1;s9=pwm2;s11=pwm2;
s1=0;s2=0;s3=0;s4=0;s6=0;s8=0;s10=0;s12=0;s13=0;s14=0;
elseif high==soc3&&low==soc7
s5=pwm1;s7=1;s9=pwm1;s11=pwm2;s13=pwm2;
s1=0;s2=0;s3=0;s4=0;s6=0;s8=0;s10=0;s12=0;s14=0;
%soc4 starts here
elseif high==soc4&&low==soc1
s3=pwm2;s7=pwm1;s8=pwm1;
s1=0;s2=0;s4=0;s5=0;s6=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc4&&low==soc2
s2=pwm2;s5=pwm2;s7=pwm1;s8=pwm1;
s1=0;s3=0;s4=0;s6=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc4&&low==soc3
s4=pwm2;s8=pwm1;s7=1;
s1=0;s2=0;s3=0;s5=0;s6=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc4&&low==soc5
s7=pwm1;s9=pwm1;s10=pwm2;
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s8=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc4&&low==soc6
s7=pwm1;s9=pwm1;s11=pwm2;s12=1;
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s8=0;s10=0;s13=0;s14=0;
elseif high==soc4&&low==soc7
s7=pwm1;s9=1;s11=pwm1;s13=pwm2;s14=pwm2;
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s8=0;s10=0;s12=0;
%soc5 starts here
elseif high==soc5&&low==soc1
s10=pwm1;s9=pwm1;s2=pwm2;s4=pwm2;s6=1;
s1=0;s3=0;s5=0;s7=0;s8=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc5&&low==soc2
s10=pwm1;s7=pwm1;s4=pwm2;s3=pwm2;
s1=0;s2=0;s5=0;s6=0;s8=0;s9=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc5&&low==soc3
s10=pwm1;s9=pwm1;s6=pwm2;s5=pwm2;
s1=0;s2=0;s3=0;s4=0;s7=0;s8=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc5&&low==soc4
s10=pwm1;s8=pwm1;s7=pwm2;
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s9=0;s11=0;s12=0;s13=0;s14=0;
elseif high==soc5&&low==soc6
s9=pwm1;s11=pwm1;s12=pwm2;
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s7=0;s8=0;s10=0;s13=0;s14=0;
elseif high==soc5&&low==soc7
s9=pwm1;s11=pwm1;s12=pwm2;s14=pwm2;
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s7=0;s10=0;s8=0;s13=0;
%soc6 starts here
elseif high==soc6&&low==soc1
s12=pwm1;s10=1;s8=pwm1;s2=pwm2;s4=pwm2;
s1=0;s3=0;s5=0;s6=0;s7=0;s9=0;s11=0;s13=0;s14=0;
elseif high==soc6&&low==soc2
s12=pwm1;s10=pwm1;s6=pwm2;s3=pwm2;s4=1;
s1=0;s2=0;s5=0;s7=0;s8=0;s9=0;s11=0;s13=0;s14=0;
elseif high==soc6&&low==soc3
s12=pwm1;s9=pwm1;s6=pwm2;s5=pwm2;
s1=0;s2=0;s3=0;s4=0;s7=0;s8=0;s10=0;s11=0;s13=0;s14=0;
elseif high==soc6&&low==soc4
s12=pwm1;s10=pwm1;s8=pwm2;s7=pwm2;
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s9=0;s11=0;s13=0;s14=0;
elseif high==soc6&&low==soc5
s12=pwm1;s10=pwm1;s9=pwm2;
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s7=0;s8=0;s11=0;s13=0;s14=0;
elseif high==soc6&&low==soc7
s11=pwm1;s13=pwm1;s14=pwm2;
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s7=0;s8=0;s9=0;s10=0;s12=0;
%soc7 starts here
elseif high==soc7&&low==soc1
s14=pwm1;s13=pwm1;s9=pwm2;s7=1;s2=pwm2;s5=1;
s1=0;s3=0;s4=0;s6=0;s8=0;s10=0;s11=0;s12=0;
elseif high==soc7&&low==soc2
s14=pwm1;s11=pwm1;s9=pwm2;s3=pwm2;s5=1;
s1=0;s2=0;s4=0;s6=0;s7=0;s8=0;s10=0;s12=0;s13=0;
elseif high==soc7&&low==soc3
s14=pwm1;s12=pwm1;s5=pwm2;s9=pwm2;s7=1;
s1=0;s2=0;s3=0;s4=0;s6=0;s8=0;s10=0;s11=0;s13=0;
elseif high==soc7&&low==soc4
s14=pwm1;s11=pwm1;s9=pwm2;s7=pwm2;
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s8=0;s10=0;s12=0;s13=0;
elseif high==soc7&&low==soc5
s14=pwm1;s12=pwm1;s10=pwm2;s9=pwm2;
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s7=0;s8=0;s11=0;s13=0;
elseif high==soc7&&low==soc6
s14=pwm1;s11=pwm2;s13=pwm1;
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s7=0;s8=0;s9=0;s10=0;s12=0;
else
s1=0;s2=0;s3=0;s4=0;s5=0;s6=0;s7=0;s8=0;s9=0;s10=0;s11=0;s12=0;s13=0;s14=0;
end
end
the code is running but it didnt balance

Sign in to comment.

More Answers (1)

You should not use while. Repalce it with if.
function [x,y] = fcn(u)
% u = battery voltage
% x=1 battery charge switch
% y=1 battery discharge switch
if (u<=0.9) % battery charging
if (u<=1.2)
x=1;
y=0;
end
elseif (u>1.2) || (u>0.9) && (u<1.2) %battery discharge
if (u>=0.9)
x=0;
y=1;
end
else
x=0;
y=0;
end
end

8 Comments

it didn't work again.
Error:
Output argument 'x' is not assigned on some execution paths.
If the battery voltage is less than 0.9, it will charge until it reaches 1.2 volts and exit this cycle.
If the battery voltage is between 0.9 and 1.2, it will discharge to 0.9, exit this cycle and go back to the first condition
Tell me for what input it din't work?
Error: Output argument 'x' is not assigned on some execution paths.
It feels like you are not showing the actual code you are running on your PC. The code in KSSV answer will assign values to output variables on all execution paths.
You need to rethink on your code...
if (u<=0.9) % battery charging
if (u<=1.2)
x=1;
y=0;
end
elseif
The above line looks ridiculous ...Now as you got to know using while is wrong and you have to use if elseif...you can proceed on your own.
14/5000
that's all the code. Sorry.. I am trying to express my purpose to you. I am not oppose you. But code not working. I hope you can help me. thank you
Hey...it is not you are opposing me...I said it because..now you understood the gist..and you can do it...
if (u <= 0.9)% battery charging
if (u <= 1.2)
x = 1;
y = 0;
end
elseif
this condition not working. 1. if is working, 2. if is not working. u initial value <=0.9 , when u is greater than 0.9, y = 1 x = 0.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!