Output argument 'fy' is not assigned on some execution paths
조회 수: 175 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Voss
2023년 7월 22일
This function returns without assigning a value to fy (and M and rills) whenever u >= 0.436.
if u>=0.436
fz=0;
x1=0;
return; % returns here. at this point fy, M and rills are unassigned
end
댓글 수: 5
Voss
2023년 7월 26일
"fz=0,so,fy,M should be zero"
fy and M are whatever you assign them to be. If you don't assign them, they are unassigned. If the function returns with them unassigned, you get the error you saw.
The code in my comment effectively sets them to zero when fz is zero, because instead of returning early, the code continues to the calculation of fy and M, which will both be zero when fz is zero.
추가 답변 (2개)
Manan Jain
2023년 7월 22일
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
Image Analyst
2023년 7월 22일
Try stepping through the code and looking at the variables. In other words use debugging like everyone else does.
Image Analyst
2023년 7월 22일
편집: Image Analyst
2023년 7월 22일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 General Applications에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!