function theText = TernText(x, y, z, theString)

% TernText -- Text for TernPlot.
%  TernText(x, y, z, theString) draws centered text
%   at the given (x, y, z) coordinates and returns
%   its handle.

% Copyright (C) 1996 Dr. Charles R. Denham, ZYDECO.
% All Rights Reserved.

% Version of 12-Sep-96 at 22:07:56.12.

if nargin == 2
   p = 1.1; q = -0.1 ./ 3;
   theVertex = lower(x);
   theString = y;
   v = [];
   if strcmp(theVertex, 'x')
      v = [p q q];
     elseif strcmp(theVertex, 'y')
      v = [q p q];
     elseif strcmp(theVertex, 'z')
      v = [q q p];
     elseif strcmp(theVertex, 'w')
      v = [q q q];
   end
   if ~isempty(v)
      TernText(v(1), v(2), v(3), theString)
   end
   return
end

if nargin == 3, theText = z; z = []; end

[x, y, z] = TernMap([x x], [y y], [z z]);

axes(gca)
h = text(x(1), y(1), z(1), theString);
set(h, 'HorizontalAlignment', 'center', ...
             'VerticalAlignment', 'middle')

if nargout > 0, theText = h; end
