function sysblock(varargin)
%SYSBLOCK  Draw a system block

%   Author(s): A. DiVergilio
%   Copyright 1986-2002 The MathWorks, Inc.

%---Default properties
p = struct(...
   'Parent',[],...
   'Position',[1 1 3 2],...
   'LineStyle','-',...
   'LineWidth',2,...
   'EdgeColor','k',...
   'FaceColor','w',...
   'Curvature',[0 0],...
   'FontName','',...
   'FontSize',12,...
   'FontWeight','normal',...
   'NameFontName','',...
   'NameFontSize',[],...
   'NameFontWeight','',...
   'Name','',...
   'Gain','',...
   'Numerator','',...
   'Denominator','',...
   'Bracket',0,...
   'Sigma',0,...
   'SigmaBounds',{{'',''}},...
   'Interpreter','tex',...
   'Tag','',...
   'Clipping','off');
plist = fieldnames(p);

%---Merge user-specified properties
for i=1:2:nargin-1
   Property = pnmatch(varargin{i},plist);
   Value = varargin{i+1};
   p.(Property) = Value;
end
if isempty(p.Parent), p.Parent = gca; end
if isempty(p.FontName), p.FontName = get(p.Parent,'DefaultTextFontName'); end
if isempty(p.NameFontName), p.NameFontName = p.FontName; end
if isempty(p.NameFontSize), p.NameFontSize = p.FontSize; end
if isempty(p.NameFontWeight), p.NameFontWeight = p.FontWeight; end

%---System box
rectangle(...
   'Parent',p.Parent,...
   'Position',p.Position,...
   'LineStyle',p.LineStyle,...
   'LineWidth',p.LineWidth,...
   'Curvature',p.Curvature,...
   'EdgeColor',p.EdgeColor,...
   'FaceColor',p.FaceColor,...
   'Tag',p.Tag,...
   'Clipping',p.Clipping);

%---Add text
x = p.Position(1)+p.Position(3)/2;
y = p.Position(2)+p.Position(4)/2;
if isempty(p.Numerator)
   %---System name only
   ty = y;
   tv = 'middle';
else
   %---Put system name below block
   ty = p.Position(2);
   tv = 'top';
   %---System equation
   equation(...
      'Parent',p.Parent,...
      'Position',[x y],...
      'LineWidth',0.5,...
      'Color',p.EdgeColor,...
      'FontName',p.FontName,...
      'FontSize',p.FontSize,...
      'FontWeight',p.FontWeight,...
      'Gain',p.Gain,...
      'Numerator',p.Numerator,...
      'Denominator',p.Denominator,...
      'Anchor','center',...
      'Bracket',p.Bracket,...
      'Sigma',p.Sigma,...
      'SigmaBounds',p.SigmaBounds,...
      'Interpreter',p.Interpreter,...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
end
%---System name
t = text(...
   'Parent',p.Parent,...
   'Position',[x ty],...
   'String',p.Name,...
   'Color',p.EdgeColor,...
   'FontName',p.NameFontName,...
   'FontSize',p.NameFontSize,...
   'FontWeight',p.NameFontWeight,...
   'HorizontalAlignment','center',...
   'VerticalAlignment',tv,...
   'Interpreter',p.Interpreter,...
   'Tag',p.Tag,...
   'Clipping',p.Clipping);
if ~isempty(p.Numerator)
   ext = get(t,'Extent');
   set(t,'Position',[x ty-ext(4)*.3]);
end
