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

조회 수: 14 (최근 30일)
satabdi bhattacharya
satabdi bhattacharya 2022년 10월 29일
답변: Star Strider 2022년 10월 29일
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.
.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by