function sumblock(varargin)
%SUMBLOCK  Draw a sum block

%   Author(s): A. DiVergilio
%   Copyright 1986-2002 The MathWorks, Inc.
%   $Revision: 1.7 $  $Date: 2002/04/10 06:42:06 $

%---Default properties
p = struct(...
   'Parent',[],...
   'Position',[1 1],...
   'Radius',1,...
   'LineWidth',2,...
   'EdgeColor','k',...
   'FaceColor','none',...
   'FontSize',12,...
   'FontWeight','normal',...
   'Label',{{'+145','-235'}},...
   'LabelRadius',[],...
   'Showsum',0,...
   '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.LabelRadius), p.LabelRadius = 2*p.Radius; end
if isempty(p.Parent), p.Parent = gca; end

%---Draw a circle
a = 0:2*pi/100:2*pi;
patch(...
   'Parent',p.Parent,...
   'XData',p.Position(1)+p.Radius*cos(a),...
   'YData',p.Position(2)+p.Radius*sin(a),...
   'LineWidth',p.LineWidth,...
   'EdgeColor',p.EdgeColor,...
   'FaceColor',p.FaceColor,...
   'Tag',p.Tag,...
   'Clipping',p.Clipping);
%---Draw summation sign if requested
if p.Showsum
   r = 0.66*p.Radius*[.8 1 1 0 1 1 .8];
   a = pi*[.2 .25 .75 0 1.25 1.75 1.8];
   line(...
      'Parent',p.Parent,...
      'XData',p.Position(1)+r.*cos(a),...
      'YData',p.Position(2)+r.*sin(a),...
      'LineWidth',p.LineWidth,...
      'Color',p.EdgeColor,...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
end
%---Add labels (for +/-)
if ~iscell(p.Label), p.Label={p.Label}; end
for n=1:length(p.Label)
   L = p.Label{n};
   str = L(1);
   ang = str2num(L(2:end))*pi/180;
   text(...
      'Parent',p.Parent,...
      'Position',p.Position + p.LabelRadius*[cos(ang) sin(ang)],...
      'String',str,...
      'FontSize',p.FontSize,...
      'FontWeight',p.FontWeight,...
      'HorizontalAlignment','center',...
      'VerticalAlignment','middle',...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
end
