Output argument 'ENA' is not assigned on some execution paths

1 view (last 30 days)
i don't know ehy it gives me this error
function [ENA,ENB,IN1,IN2,IN3,IN4] = fcn(Ms,Rs,Ls)
persistent Ms1 Rs1 Ls1
if isempty(Ms1)
Ms1=0;
Ls1=0;
Rs1=0;
end
if (Ms==0 && Rs==0 && Ls==0)
if (Ms1==1 && Rs1==0 && Ls1==0)
ENA=1; ENB=1;
IN1=0; IN2=1;
IN3=1; IN4=0;
elseif (Ms1==1 && Rs1==1 && Ls1==0)
ENA=1.5; ENB=0;
IN1=0; IN2=1;
IN3=1; IN4=0;
elseif (Ms1==1 && Rs1==0 && Ls1==1)
ENA=0; ENB=1.5;
IN1=0; IN2=1;
IN3=1; IN4=0;
elseif (Ms1==0 && Rs1==1 && Ls1==0)
ENA=1.5; ENB=0;
IN1=0; IN2=1;
IN3=1; IN4=0;
elseif (Ms1==0 && Rs1==0 && Ls1==1)
ENA=0; ENB=1.5;
IN1=0; IN2=1;
IN3=1; IN4=0;
end
elseif (Ms==1 && Rs==0 && Ls==0)
ENA=1; ENB=1;
IN1=0; IN2=1;
IN3=1; IN4=0;
elseif (Ms==1 && Rs==1 && Ls==0)
ENA=1.5; ENB=0;
IN1=0; IN2=1;
IN3=1; IN4=0;
elseif (Ms==1 && Rs==0 && Ls==1)
ENA=0; ENB=1.5;
IN1=0; IN2=1;
IN3=1; IN4=0;
elseif (Ms==0 && Rs==1 && Ls==0)
ENA=1.5; ENB=0;
IN1=0; IN2=1;
IN3=1; IN4=0;
elseif (Ms==0 && Rs==0 && Ls==1)
ENA=0; ENB=1.5;
IN1=0; IN2=1;
IN3=1; IN4=0;
elseif (Ms==1 && Rs==1 && Ls==1)
ENA=0; ENB=0;
IN1=0; IN2=0;
IN3=0; IN4=0;
end
Ms1=Ms;
Ls1=Ls;
Rs1=Rs;
end

Answers (1)

dpb
dpb on 31 Jul 2022
Edited: dpb on 31 Jul 2022
1, You don't have an else clause so it is possible to select a set of inputs the combination of which will not satisfy any of the conditions that are in the if...elseif...end construct.
The simplest robust solution would be to add the else clause and handle it appropriately if that were to ever happen that the condition isn't met -- maybe that should generate an error?
Alternatively, assign some default set of values to the outputs on entry before the if clause construct; the values of which are overwritten unless the above case were to occur.
Lastly, you could test the inputs to be in their allowable ranges via assert or some other logic.
2. You don't have a case inside the first if clause for the initial default values of the persistent variables all being zero -- and in that case, they all remain 0.
You'll have to decide what that case should do for initialization or, as above, test that it's an invalid input combination--although you allow it in the first if so apparently that's ok and you just missed getting an assignment to move them off their initial values.
3. I didn't work through all the other possibilities...but that's at least two ways/whys...
4. Your code at present has no way to programmatically be able to reset the persistent variables other than by a clear statement on the function name.
  2 Comments
Image Analyst
Image Analyst on 31 Jul 2022
Edited: Image Analyst on 31 Jul 2022
Like dpb said, to avoid the warning, assign some defaults, like 0 or -1 or even null []. Do this as the very first part of your function:
ENA = [];
ENB = [];
IN1 = [];
IN2 = [];
IN3 = [];
IN4 = [];
dpb
dpb on 31 Jul 2022
Edited: dpb on 31 Jul 2022
That alone will prevent the error/warning; doesn't fix logic error/oversight, however.
Six outputs are a bunch; I'd also suggest changing ENA|ENB to an array EN and likewise with IN -- subscripting them instead of multiple variables.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!