简体   繁体   中英

If Statement Matlab Function on Simulink

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"

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.

You should probably replace that line:

elseif in_Motor<0

with a simple else .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM