Variable "Flag" is not fully defined in some execution paths. Why?

2 次查看(过去 30 天)
function [Gs1, Gs2, Gs3] = mpp(Ppv, Po, T, Ipv, Vbat)
persistent Flag0
if isempty(Flag0)
Flag0 = 1; Flag = 1;
end
Vbnom = 45; Vbmax = 50;
if(mod(T,0.02)== 0)
while (Flag0 == 1)
while (Vbat <= Vbnom)
Gs1 = 1;
Gs2 = 1;
Gs3 = 0;
Flag = 2;
Flag0 = Flag;
return
end
while (Vbat > Vbnom)
Gs1 = 1;
Gs2 = 0;
Gs3 = 1;
Flag = 3;
Flag0 = Flag;
return
end
end
while (Flag0 == 2)
while (Ipv <= 0)
Gs1 = 0;
Gs2 = 0;
Gs3 = 1;
Flag = 1; Flag0 = Flag;
return
end
while (Ipv > 0)
while (Ppv >= Po)
while (Vbat >= Vbmax)
Gs1 = 1;
Gs2 = 0;
Gs3 = 1;
Flag = 3;
Flag0 = Flag;
return
end
while (Vbat < Vbmax)
Gs1 = 1;
Gs2 = 1;
Gs3 = 0;
Flag = 1; Flag0 = Flag;
return
end
end
while (Ppv < Po)
Gs1 = 1;
Gs2 = 0;
Gs3 = 1;
Flag = 3; Flag0 = Flag;
return
end
end
end
while (Flag0 == 3)
while (Ipv <= 0)
Gs1 = 0;
Gs2 = 0;
Gs3 = 1;
Flag = 1; Flag0 = Flag;
return
end
while (Ipv > 0)
while (Ppv > Po)
Gs1 = 1;
Gs2 = 1;
Gs3 = 0;
Flag = 2; Flag0 = Flag;
return
end
while (Ppv <= Po)
Gs1 = 1;
Gs2 = 0;
Gs3 = 1;
Flag = 3; Flag0 = Flag;
return
end
end
end
while (Flag0 > 3) || (Flag0 < 0)
Gs1 = 1;
Gs2 = 0;
Gs3 = 1;
Flag = 1; Flag0 = Flag;
end
else
Flag = 1;
Gs1 = 1;
Gs2 = 1;
Gs3 = 0;
end
Flag0 = Flag;

回答(1 个)

Star Strider
Star Strider 2022-10-29
Because there are conditional statements in the code, and not all of them set the ‘Flag’ variable.
One way to avoid that is to set it to something at the beginning of the code, for example —
function [Gs1, Gs2, Gs3] = mpp(Ppv, Po, T, Ipv, Vbat)
Flag = NaN;
... REST OF THE CODE ...
end
or whatever default value for it makes sense in context.
.

类别

Help CenterFile Exchange 中查找有关 Simulink Environment Customization 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by