Output argument m_dot_B is not assigned on some execution paths

3 views (last 30 days)
Hello guys, I have a question about Matlab. When i run the simulation, Matlab give instruction " Output argument is not assigned on some execution path. Below my coding that i using in matlab and im using "matlab function" block. Hope you can help me Thank you
function m_dot_B = fcn(Av, PB) %#codegen
k = 1.4; Cf=0.7; T=294.5; C1=0.0404; C2=0.1562; Pcr=0.528; Ps=5*exp(5);
if PB/Ps <= Pcr
m_dot_B = Cf*Av*C1*(Ps/T^(1/2));
elseif PB/Ps > Pcr
m_dot_B = Cf*Av*C2*(Ps/T^(1/2)*((PB/Ps)^(1/k))*(1-(PB/Ps)^((k-1)/k))^(1/2));
end

Accepted Answer

Image Analyst
Image Analyst on 17 Nov 2013
It's possible that neither of your if tests will be true, and if that happens, then fcn would return without having set anything for m_dot_B. A more robust way to write the code would be like this:
function m_dot_B = fcn(Av, PB) %#codegen
% Initialize output in case something goes wrong.
m_dot_B = 0;
try
k = 1.4;
Cf=0.7;
T=294.5;
C1=0.0404;
C2=0.1562;
Pcr=0.528;
Ps=5*exp(5);
if PB/Ps <= Pcr
m_dot_B = Cf*Av*C1*(Ps/T^(1/2));
else
m_dot_B = Cf*Av*C2*(Ps/T^(1/2)*((PB/Ps)^(1/k))*(1-(PB/Ps)^((k-1)/k))^(1/2));
end
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage);
end

More Answers (0)

Categories

Find more on Software Development Tools in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!