function [sys, x0, str, ts] = quantize(t, x, u, flag, q)
%VLIMINTM Vectored quantizer implementation.
%	This M-file is an example of a quantizer S-Function.  The input
%	is quantized into steps as specified by the quantization interval
%	parameter, q.
%
%	Copyright (c) 1990-94 by The MathWorks, Inc.
%	Rick Spada 6-6-94

flag = abs(flag);

if (flag == 3)
  
  signs = sign(u);
  signs(~signs) = ones(size(signs(~signs)));
  sys = q .* floor(abs(u./q) + 0.5) .* signs;
  
elseif (flag == 0)

  % if q is a vector, it defines the number of inputs and outputs,
  % otherwise, the number is dynamic

  if (length(q) > 1)
    
    numOutputs = length(q);   % dynamic number of outputs
    numInputs = length(q);    % dynamic number of inputs
    
  else
  
    numOutputs = -1;         % dynamic number of outputs
    numInputs = -1;          % dynamic number of inputs

  end
  
  sys(1) = 0;           % no continuous states
  sys(2) = 0;           % no discrete states
  sys(3) = numOutputs;  % number of outputs
  sys(4) = numInputs;   % number of inputs
  sys(5) = 0;           % unused
  sys(6) = 1;           % has direct feedthrough
  sys(7) = 1;           % one sample time
  
  x0 = [];
  str = [];
  ts = -1;              % inherited sample time
else
  sys = [];
end
