Output argument 'fy' is not assigned on some execution paths

176 Ansichten (letzte 30 Tage)
翰旭
翰旭 am 22 Jul. 2023
Kommentiert: Voss am 26 Jul. 2023
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

Akzeptierte Antwort

Voss
Voss am 22 Jul. 2023
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 Kommentare
翰旭
翰旭 am 26 Jul. 2023
"returns here. at this point fy, M and rills are unassigned",fz=0,so,fy,M should be zero;so why fy, M, rills are unassigned?
Voss
Voss am 26 Jul. 2023
"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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Manan Jain
Manan Jain am 22 Jul. 2023
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 Kommentare
翰旭
翰旭 am 22 Jul. 2023
this image can you know the information
Image Analyst
Image Analyst am 22 Jul. 2023
Try stepping through the code and looking at the variables. In other words use debugging like everyone else does.

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 22 Jul. 2023
Bearbeitet: Image Analyst am 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.

Kategorien

Mehr zu General Applications finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by