function detach(varargin)
% DETACH Display separating line with text in the middle
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The function creates a separating text line on the screen and in records
% (diaries). User defined text (if any) is placed in the middle of the line.
% It may contain arbitrary information. Vertical position of the separator
% is controlled by last two parameters.
% The length of the separator is text-length independent.
% Calls:
% detach % default values of params will be used
% detach(txt)
% detach(txt,chr)
% detach(txt,chr,len)
% detach(txt,chr,len,before)
% detach(txt,chr,len,before,after)
% Input arguments:
% txt inserted text; '' default (no text)
% chr separating character:
% '-' default in the first call,
% the last used character otherwise,
% len half-length of the line, default 25 characters
% Examples:
% detach
% detach('EXAMPLE')
% detach(date,'=')
% detach([mfilename ', ' date],'@')
% % Outputs:
% % --------------------------------------------------
% % -------------------- EXAMPLE ---------------------
% % ================== 07-Aug-2008 ===================
% % @@@@@@@@@@@@@@ dettest, 07-Aug-2008 @@@@@@@@@@@@@@
% Miroslav Balda
% miroslav AT balda DOT cz
% 2005-02-24 v 1.0 First version of the function separator.
% 2006-03-30 v 1.1 Changed common variables into persistent ones.
% 2008-08-07 v 1.2 Reconstructed into the function detach.n.
% Complemented 2 parameters for vertical gaps
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
persistent len_ chr_ before_ after_
ini = nargin>0 && strcmp(varargin{1},'RESET');
if isempty(chr_) || ini
chr_='-'; % default character
len_=25; % default separator half-length
before_ = 1; % number of free lines before separating line
after_ = 1; % number of free lines after separating line
end
if ini, return, end
if nargin<1
txt = '';
else
txt = varargin{1}; % text
end
if nargin>1 && ~isempty(varargin{2})
chr_ = varargin{2}; % character
end
if nargin>2 && ~isempty(varargin{3})
len_ = varargin{3}; % half-length of separator
end
if nargin>3 && isreal(varargin{4})
before_ = varargin{4}; % number of lines before separator
end
if nargin>4 && isreal(varargin{5})
after_ = varargin{5}; % number of lines after separator
end
if length(txt)>0, txt=[' ' txt ' ']; end
L = 2*len_;
n = fix((L-length(txt))/2);
Line = char(ones(1,n)*chr_);
Line = [Line txt Line];
if length(Line)<L
Line = [Line chr_];
end
up = char(10*ones(1,before_));
lo = char(10*ones(1,after_));
fprintf('%s',[up Line lo]); % Separator output