function [stat,post] = blkss(path,prop,bstr,bint,bflt,parent,name,catalog,ws,node,sbSample)
%BLKSS Defines a system specified in state-space form.
%
% See also BLKTF, BLKZP, BLKDF.

% Initial coding by A S Bozin, 6-7-97
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.6 $ $Date: 2000/06/14 16:11:10 $

   stat = 0;			   		   % Error flag
   nInputs = bint(5);
   nOutputs = bint(6);
   nStates = bint(7);
   %
   % Add mux and demux blocks if necessary
   %
   [post,rect] = blkio('port',nInputs,nOutputs,prop,path);
   %
   % Get the system matrix and initial conditions
   %
   indStr = bint(17)+1;
   indInt = bint(15)+1;
   symInd = zeros(2,1);				   % Block parameter flags
   str = cell(3,1);
   str{3} = mat2char(nStates);
   mode = bint(indInt);
   if (bitand(mode,8))
      ind = mfindstr(bstr,indStr,'|');
      symInd(1) = 1;
      str{1} = bstr(indStr:ind-1);
      indStr = ind+1;
   end
   if (bitand(mode,16))
      ind = mfindstr(bstr,indStr,'|');
      symInd(2) = 1;
      str{2} = bstr(indStr:ind-1);
   end
   %
   % Get block parameter variables
   %
   [symInd,str] = mgetscope(symInd,str,catalog,ws,node);
   if (symInd(1) == 0)
      indFlt = bint(16);
      indInt = indInt+1;
      nnzic = bint(indInt);
      cRange = 1:nnzic;
      indInt = indInt+nnzic;
      x0 = zeros(nStates,1);
      if (~isempty(x0))
	 x0(bint(indInt+cRange)) = bflt(indFlt+cRange);
      end
      str{1} = mat2char(x0);
   end
   if (symInd(2) == 0)
      indInt = bint(18);
      indInt = bint(indInt+1)+indInt;		   % Skip icon data
      indFlt = bint(19);
      cRange = 1:4;
      sysDims = bint(indInt+cRange);
      nCoeffs = sum(sysDims);
      indInt = indInt+4;
      iRange = 1:2*nCoeffs;
      cRange = 1:nCoeffs;
      rDims = [nStates;nStates;nOutputs;nOutputs];
      cDims = [nStates;nInputs;nStates;nInputs];
      [a,b,c,d] = mgetsys(sysDims,rDims,cDims,bint(indInt+iRange),bflt(indFlt+cRange));
      str{2} = mat2char([a b;c d]);
   end
   %
   % State space block (can be continuous or discrete)
   %
   if (any(sbSample))
      bh = add_block('libsb2sl/DYN/DStateSpace',path,...
		     'ForegroundColor',prop.fgColor,...
		     'BackgroundColor',prop.bgColor,...
		     'Orientation',prop.orientation,...
		     'DropShadow',prop.border,...
		     'Position',rect,...
		     'NX',str{3},...
		     'SYS',str{2},...
		     'X0',str{1},...
		     'TS',mat2char(sbSample));
   else
      bh = add_block('libsb2sl/DYN/CStateSpace',path,...
		     'ForegroundColor',prop.fgColor,...
		     'BackgroundColor',prop.bgColor,...
		     'Orientation',prop.orientation,...
		     'DropShadow',prop.border,...
		     'Position',rect,...
		     'NX',str{3},...
		     'SYS',str{2},...
		     'X0',str{1});
   end
   %
   % Connect the current state-space block with the mux and demux blocks
   %
   blkio('line',nInputs,nOutputs,post,parent,name);
   return					   % Last card of BLKSS

function varargout = mgetsys(cnum,rows,cols,indices,coeffs)
%MGETSYS
%
%

   n = size(cnum,2);
   varargout = cell(n,1);
   ind = 0;
   clb = 0;
   cub = 0;
   for (k = 1:n)
      nCoeffs = cnum(k);
      if (nCoeffs == 0)
	 sys = sparse(rows(k),cols(k));
      else
	 iRange = 1:nCoeffs;
	 i = indices(ind+iRange);
	 ind = ind+nCoeffs;
	 j = indices(ind+iRange);
	 clb = cub+1;
	 cub = cub+nCoeffs;
	 sys = sparse(i,j,coeffs(clb:cub),rows(k),cols(k));
	 ind = ind+nCoeffs;
      end
      varargout{k} = full(sys);
   end
   return					   % Last card of MGETSYS
