Output argument 'fy' is not assigned on some execution paths
Show older comments
function [fz,x1,fy,M,rills] = fcn(u,m,w,v,r)
if u>=0.436
fz=0;
x1=0;
return;
end
fz = 2*(1+0.4*-m)*739130*(0.436-u);
x1=0.436-u;
r0=2*r/3;
rills=(w*r0-v)/max([abs(v) abs(w*r0) 0.1]);
if (0.13<rills)&&(rills<=1)
fy=(0.77-0.32*rills)*fz;
M=(0.77-0.32*rills)*fz*r0;
elseif -0.13<rills&&rills<=0.13
fy=(5.62*rills)*fz;
M=(5.62*rills)*fz*r0;
else
fy=0.5*fz;
M=0.5*fz*r0;
end
Accepted Answer
More Answers (2)
Manan Jain
on 22 Jul 2023
0 votes
Hi!
The warning message "Output argument 'fy' is not assigned on some execution paths" means that there are scenarios in the function where the variable fy might not be assigned a value. In MATLAB, all output variables in a function must be assigned a value before the function returns. If there are paths in the code where fy might not be assigned, it could lead to unexpected behavior when the function is called.
The warning occurs in the case where the condition (0.13 < rills) && (rills <= 1) and the condition (-0.13 < rills) && (rills <= 0.13) are both false. In this case, the fy variable won't be assigned a value, and it can lead to an issue when the function is called with the expectation that fy will always be an output.
To fix this warning, you should make sure that fy is assigned a value in all possible execution paths.
I hope this helps!
Thanks
4 Comments
翰旭
on 22 Jul 2023
What makes you think the code is not executing the 3rd condition?
The if-else conditions are not the problem in your code.
If you want a solution, you will have to provide the information I have asked above in the comment.
rills=2;
if (0.13<rills)&&(rills<=1)
disp('if')
%fy=(0.77-0.32*rills)*fz;
%M=(0.77-0.32*rills)*fz*r0;
elseif -0.13<rills&&rills<=0.13
disp('elseif')
%fy=(5.62*rills)*fz;
%M=(5.62*rills)*fz*r0;
else
disp('else')
%fy=0.5*fz;
%M=0.5*fz*r0;
end
翰旭
on 22 Jul 2023
Image Analyst
on 22 Jul 2023
Try stepping through the code and looking at the variables. In other words use debugging like everyone else does.
Image Analyst
on 22 Jul 2023
Edited: Image Analyst
on 22 Jul 2023
If you're not going to assign the outputs in all scenarios, you can initialize the variables to null. Actually I do this by habit for all my functions just as a good habit:
function [fz, x1, fy, M, rills] = fcn(u, m, w, v, r)
% Initialize outputs to null.
fz = [];
x1 = [];
fy = [];
M = [];
rills = [];
% Then the rest of the code.
that way at least the variables will have some value, even though it's null, and you will avoid the error. Later if you get null returned in your main calling program and were not expecting that you can step through it with the debugger and figure out what if blocks you did and did not go into and figure out why some variable never got assigned to something other than null.
Of course you don't have to initialize them to null. You can assign them whatever values you want.
Categories
Find more on Entering Commands in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!