MATLAB: Output argument ‘y’ is not assigned on some execution paths

codeMATLABmatlab function

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

Best Answer

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.
Related Question