% function [mattype,rowd,cold,num] = minfo(mat)
%  or
% function  minfo(mat)
%
%   Return information about a matrix and provides a consistency 
%   check for CONSTANT, SYSTEM and VARYING matrices.
%
%   See also: PCK, PSS2SYS, SYS2PSS, UNPCK, and VUNPCK.

function [mattype,rowd,cold,num] = minfo(mat)
  if nargout == 2 | nargout == 3 | nargin ~= 1
    disp('usage: [mattype,rowdata,coldata,num] = minfo(mat)');
  else
    if isempty(mat)
      flg = 3;
    else
      [nr,nc] = size(mat);
      if nr == 1 | nc == 1
        flg = -1;
      else
        flg = 0;
        states = mat(1,nc);
        if any(real(mat(2:nr-1,nc))) | any(imag(mat(2:nr-1,nc))) | ...
          any(real(mat(nr,1:nc-1))) | any(imag(mat(nr,1:nc-1)))
          flg = -2;
        elseif states < 0 | mat(nr,nc) ~= -inf
          flg = -2;
        elseif  floor(states) ~= ceil(states)
          flg = -2;
        elseif states >= min([nr,nc])-1
          flg = -2;
        end
        if flg == 0
          flg = 1;
        else
          npts = mat(nr,nc-1);
          if  floor(npts) ~= ceil(npts) | npts <= 0
            flg = -3;
          elseif npts >= nr | mat(nr,nc) ~= inf
            flg = -3;
          elseif  any(real(mat(npts+1:nr-1,nc))) | ...
             any(imag(mat(npts+1:nr-1,nc))) | any(real(mat(nr,1:nc-2))) | ...
             any(imag(mat(nr,1:nc-2)))
            flg = -3;
          end
          if flg == -2
            nrr = round((nr-1)/npts);
            if nrr*npts == nr-1
              flg = 2;
            end
          end
        end
      end
    end
  end
 if nargout ~= 4
   if flg < 0
     disp([int2str(nr) ' rows  ' int2str(nc) ' cols: regular MATLAB matrix'])
   elseif flg == 1
     no = nr-1-states;
     ni = nc-1-states;
     lab1 = ['system:   '];
     lab2 = [int2str(states) ' states     '];
     lab3 = [int2str(no) ' outputs     '];
     lab4 = [int2str(ni) ' inputs     '];
     disp([lab1 lab2 lab3 lab4])
   elseif flg == 2
     cols=nc-1;
     lab1 = ['varying:   '];
     lab2 = [int2str(npts) ' pts     '];
     lab3 = [int2str(nrr) ' rows     '];
     lab4 = [int2str(cols) ' cols     '];
     disp([lab1 lab2 lab3 lab4])
   elseif flg == 3
     disp(['empty matrix']);
   end
 else
   if flg < 0
%    constant matrix
     mattype = 'cons';
     rowd = nr;
     cold = nc;
     num = 0;
   elseif flg == 1
%    system matrix
     mattype = 'syst';
     rowd = nr-1-states;
     cold = nc-1-states;
     num = states;
   elseif flg == 2
%    varying matrix
     mattype = 'vary';
     rowd = nrr;
     cold = nc-1;
     num = npts;
   elseif flg == 3
%    empty matrix
     mattype = 'empt';
     rowd = 0;
     cold = 0;
     num = 0;
   end
 end
%
% Copyright MUSYN INC 1991,  All Rights Reserved
