MATLAB: Operands to the || and && operators must be convertible to logical scalar values.

MATLAB

I am building motor model and using this m file but I can't solve this error
function y=com(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
please help me quickly

Best Answer

Your code has a problem if pos or Ia or Ib or Ic are not scalars. In such a case, (pos>0) or (Ic<=0) or (Ib<=0) or similar would be vectors or arrays. The && and || operators are only defined for scalars. The similar non-scalar operators are & and | .
However, if you were to just replace all of your && and || with & and | to avoid these problems, you would likely not get the result you want. When you have vectors or arrays of logical values in an if then the if is not considered true unless all of the logical values are non-zero (true). So for example if pos was a vector then
if pos < 0
would be true only if all pos were negative, as if the test were
if all(pos(:) < 0)
Chances are that you need to learn how to do logical indexing to select elements of the vectors to wor k with.
I have the suspicion that your inputs are all vectors of the same length. If so then you need to work with corresponding elements. For example,
elseif((pos>=60)&&(pos<120)&&(Ib<=0)&&(Ia<0))
y=[0.5*(Vs+ea-2*eb+ec),0.5*(Vs-ea+2*eb-ec)];
might have to become
mask = (pos>=60) & (pos<120) & (Ib<=0) & (Ia<0);
y(1, mask) = 0.5*(Vs(mask) + ea(mask) - 2*eb(mask) + ec(mask));
y(2, mask) = 0.5*(Vs(mask) - ea(mask) + 2*eb(mask) - ec(mask));
Related Question