Output argument 'x' is not assigned on some execution paths.
Show older comments
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
More Answers (1)
KSSV
on 30 Oct 2020
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
Serdar GÜNAY
on 30 Oct 2020
KSSV
on 30 Oct 2020
Tell me for what input it din't work?
Serdar GÜNAY
on 30 Oct 2020
Ameer Hamza
on 30 Oct 2020
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.
KSSV
on 30 Oct 2020
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.
Serdar GÜNAY
on 30 Oct 2020
KSSV
on 30 Oct 2020
Hey...it is not you are opposing me...I said it because..now you understood the gist..and you can do it...
Serdar GÜNAY
on 30 Oct 2020
Categories
Find more on Sources in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!