Azzera filtri
Azzera filtri

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

125 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Voss
Voss il 22 Lug 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 Commenti
翰旭
翰旭 il 26 Lug 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 il 26 Lug 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.

Accedi per commentare.

Più risposte (2)

Manan Jain
Manan Jain il 22 Lug 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 Commenti
Image Analyst
Image Analyst il 22 Lug 2023
Try stepping through the code and looking at the variables. In other words use debugging like everyone else does.

Accedi per commentare.


Image Analyst
Image Analyst il 22 Lug 2023
Modificato: Image Analyst il 22 Lug 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.

Categorie

Scopri di più su Programmatic Model Editing in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by