0

I am trying to made my own Matlab function to use in Simulink but I have not success. It is a simple If statement with one input and three output values,all of them integer, here the code:

function [ PWM,INA,INB ]  = VNH5019(in_Motor)
if in_Motor ==0
   INA=0;
   INB=0;
   PWM=0;
elseif in_Motor>0
    if in_Motor>255
    in_motor=255;
    end
      INA=1;
      INB=0;
      PWM=in_Motor;
elseif in_Motor<0
   if  in_Motor<-255
       in_motor=-255;
   end
   INA=0;
   INB=1;
   PWM=-in_Motor;
end

And here the error:

Output argument 'PWM' is not assigned on some execution paths.

Function 'MATLAB Function' (#38.28.35), line 1, column 29:
"VNH5019"

2 Answers 2

1

You should probably replace that line:

elseif in_Motor<0

with a simple else.

Sign up to request clarification or add additional context in comments.

2 Comments

If that works if because in that way you tell the compiler that, for sure there is a point where the outputs will be assigned. Probably this solution will work, however I think it is a good practise to initialize variables with a default value.
@AnderBiguri Yes, I agree. I upvoted your answer for that reason.
1

Try to assing a value to the variables before the ifs. Simulink needs values to be always defined in this type of block functions and it seems that in yours they are, but the compiler thinks they are not. So before any if, asign some value to your outputs.

It will probably work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.