Output argument 'y' is not assigned on some execution paths
12 views (last 30 days)
Show older comments
Hello, I wrote this code in MATLAB Function and I get this error " Output argument 'y' is not assigned on some execution paths. Function 'Commutation and inverter/Commutation/MATLAB Function' (#41.11.31), line 1, column 12: "commutation_inverter". I don'y know what I'm missing. I hope you can help me.
Thank you !
function y=commutation_inverter(pos,Vs,ea,eb,ec,Ia,Ib,Ic)
if (pos>=0)&&(pos<60)&&(Ic<=0)&&(Ib<=0)
y = [Vs,0.5*(-Vs+ea+eb-2*ec)];
elseif (pos>=0)&&(pos<60)&&(Ic>0)&&(Ib<=0)
y = [Vs,0];
elseif (pos>=0)&&(pos<60)&&(Ic>=0)&&(Ib>0)
y = [Vs,0.5*(-Vs+ea+eb-2*ec)];
elseif (pos>=0)&&(pos<60)&&(Ic<0)&&(Ib>0)
y = [Vs,-Vs];
elseif(pos>=60)&&(pos<120)&&(Ib>=0)&&(Ia>=0)
y=[0.5*(Vs+ea-2*eb+ec),0.5*(Vs-ea+2*eb-ec)];
elseif(pos>=60)&&(pos<120)&&(Ib<0)&&(Ia>=0)
y=[0,Vs];
elseif(pos>=60)&&(pos<120)&&(Ib<=0)&&(Ia<0)
y=[0.5*(Vs+ea-2*eb+ec),0.5*(Vs-ea+2*eb-ec)];
elseif(pos>=60)&&(pos<120)&&(Ib>0)&&(Ia<0)
y=[Vs,0];
elseif(pos>=120)&&(pos<180)&&(Ia<=0)&&(Ic<=0)
y=[0.5*(-Vs+2*ea-eb-ec),Vs];
elseif(pos>=120)&&(pos<180)&&(Ia>0)&&(Ic<=0)
y=[-Vs,Vs];
elseif(pos>=120)&&(pos<180)&&(Ia>=0)&&(Ic>0)
y=[0.5*(-Vs+2*ea-eb-ec),Vs];
elseif(pos>=120)&&(pos<180)&&(Ia<0)&&(Ic>0)
y=[0,Vs];
elseif(pos>=180)&&(pos<240)&&(Ic>=0)&&(Ib>=0)
y = [-Vs,0.5*(Vs+ea+eb-2*ec)];
elseif(pos>=180)&&(pos<240)&&(Ic<0)&&(Ib>=0)
y = [-Vs,0];
elseif(pos>=180)&&(pos<240)&&(Ic<=0)&&(Ib<0)
y = [-Vs,0.5*(Vs+ea+eb-2*ec)];
elseif(pos>=180)&&(pos<240)&&(Ic>0)&&(Ib<0)
y = [-Vs,Vs];
elseif(pos>=240)&&(pos<300)&&(Ib<=0)&&(Ia<=0)
y=[0.5*(-Vs+ea-2*eb+ec),0.5*(-Vs-ea+2*eb-ec)];
elseif(pos>=240)&&(pos<300)&&(Ib>0)&&(Ia<=0)
y=[0,-Vs];
elseif(pos>=240)&&(pos<300)&&(Ib>=0)&&(Ia>0)
y=[0.5*(-Vs+ea-2*eb+ec),0.5*(-Vs-ea+2*eb-ec)];
elseif(pos>=240)&&(pos<300)&&(Ib<0)&&(Ia>0)
y=[-Vs,0];
elseif(pos>=300)&&(pos<360)&&(Ia>=0)&&(Ic>=0)
y=[0.5*(Vs+2*ea-eb-ec),-Vs];
elseif(pos>=300)&&(pos<360)&&(Ia<0)&&(Ic>=0)
y=[Vs,-Vs];
elseif(pos>=300)&&(pos<360)&&(Ia<=0)&&(Ic<0)
y=[0.5*(Vs+2*ea-eb-ec),-Vs];
elseif(pos>=300)&&(pos<360)&&(Ia>0)&&(Ic<0)
y=[0,-Vs];
end
Accepted Answer
Adam
on 19 Oct 2018
Edited: Adam
on 19 Oct 2018
I can't be bothered to manually check every single if condition, but that error is telling you that with some inputs you gave it the function has returned without y having been set.
Given that your function body is entirely one massive if-elseif statement with no else at the end this would seem to imply that the union of all your logical cases in your statement does not cover every possibility so it fails every test and just drops through to the end of the function without setting y to anything.
Either add an else to the bottom of the if-else statement with some default value or initialise it before the if statement or check through every single if-else condition and work out what is falling through the gaps. Given this must come from a specific call you are making you can just put a breakpoint in or use the pause/stop on errors option to be able to step through the code. In that specific case find the statement you would expect it to fall into and see why it is not doing.
For your own clarity and helping you find errors like this it may help you to split your if into multiple blocks when you have so many if-else statements.
e.g. a block for the pos >= 0 condition which will contain a nested if-else statement, then another block for pos >= 60 etc. It may help work out what combination of parameters is not being caught by any of your conditions.
1 Comment
Walter Roberson
on 19 Oct 2018
"that error is telling you that with some inputs you gave it the function has returned without y having been set."
Not exactly: it is saying that there are values for which the output is not defined. It is a compile time message, not a run time message. Because of that, the debug approach is not sufficient, since the compile is analyzing including cases that might never show up in practice.
More Answers (1)
Walter Roberson
on 19 Oct 2018
Edited: Walter Roberson
on 19 Oct 2018
y is not assigned to for negative pos.
I did not bother to track down where there are any other cases.
1 Comment
Walter Roberson
on 19 Oct 2018
fail if pos is nan
fail if pos < 0
fail if pos >= 360
fail if pos > 0 & pos < 120 & Ib is nan
fail if pos >= 120 & pos < 240 & Ic is nan
... and others.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!