MATLAB Answers

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

17 views (last 30 days)
Serdar GÜNAY
Serdar GÜNAY on 30 Oct 2020
Commented: Serdar GÜNAY on 4 Nov 2020 at 8:22
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

  0 Comments

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 30 Oct 2020
Edited: Ameer Hamza on 30 Oct 2020
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.

  5 Comments

Show 2 older comments
Serdar GÜNAY
Serdar GÜNAY on 30 Oct 2020
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
Ameer Hamza
Ameer Hamza on 30 Oct 2020
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
Serdar GÜNAY
Serdar GÜNAY on 4 Nov 2020 at 8:22
I didn't know about this problem in Matlab. Thank you. My problem is solved.
Good work..

Sign in to comment.

More Answers (1)

KSSV
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

Show 5 older comments
Serdar GÜNAY
Serdar GÜNAY on 30 Oct 2020
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
KSSV
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
Serdar GÜNAY on 30 Oct 2020
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!

Translated by