Home > matGeom > geom2d > drawLabels.m

drawLabels

PURPOSE ^

Draw labels at specified positions.

SYNOPSIS ^

function varargout = drawLabels(varargin)

DESCRIPTION ^

 Draw labels at specified positions.
   
   drawLabels(X, Y, LBL)
   Draws labels LBL at positions given by X and Y.
   LBL can be either a string array, or a number array. In this case,
   string are created by using sprintf function, using the '%.2f' format.

   drawLabels(POS, LBL)
   Draws labels LBL at position specified by POS, where POS is a N-by-2
   numeric array. 

   drawLabels(..., NUMBERS, FORMAT)
   Creates labels using sprintf function, with the mask given by FORMAT 
   (e.g. '%03d' or '5.3f'), and the corresponding values.

   drawLabels(..., PNAME, PVALUE)
   Specifies additional parameters to the created labels. See 'text'
   properties for available values.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = drawLabels(varargin)
0002 % Draw labels at specified positions.
0003 %
0004 %   drawLabels(X, Y, LBL)
0005 %   Draws labels LBL at positions given by X and Y.
0006 %   LBL can be either a string array, or a number array. In this case,
0007 %   string are created by using sprintf function, using the '%.2f' format.
0008 %
0009 %   drawLabels(POS, LBL)
0010 %   Draws labels LBL at position specified by POS, where POS is a N-by-2
0011 %   numeric array.
0012 %
0013 %   drawLabels(..., NUMBERS, FORMAT)
0014 %   Creates labels using sprintf function, with the mask given by FORMAT
0015 %   (e.g. '%03d' or '5.3f'), and the corresponding values.
0016 %
0017 %   drawLabels(..., PNAME, PVALUE)
0018 %   Specifies additional parameters to the created labels. See 'text'
0019 %   properties for available values.
0020 %
0021 
0022 %   ---------
0023 %   author : David Legland
0024 %   INRA - TPV URPOI - BIA IMASTE
0025 %   created the 15/12/2003.
0026 %
0027 
0028 %   HISTORY
0029 %   09/03/2007: (re)implement it...
0030 %   2011-10-11 add management of axes handle
0031 
0032 % check if enough inputs are given
0033 if isempty(varargin)
0034     error('wrong number of arguments in drawLabels');
0035 end
0036 
0037 % extract handle of axis to draw on
0038 if isscalar(varargin{1}) && ishandle(varargin{1})
0039     ax = varargin{1};
0040     varargin(1) = [];
0041 else
0042     ax = gca;
0043 end
0044 
0045 % process input parameters
0046 var = varargin{1};
0047 if size(var, 2) == 1
0048     % coordinates given as separate arguments
0049     if length(varargin) < 3
0050         error('wrong number of arguments in drawLabels');
0051     end
0052     px  = var;
0053     py  = varargin{2};
0054     lbl = varargin{3};
0055     varargin(1:3) = [];
0056 else
0057     % parameters given as a packed array
0058     if length(varargin) < 2
0059         error('wrong number of arguments in drawLabels');
0060     end
0061     px  = var(:,1);
0062     py  = var(:,2);
0063     lbl = varargin{2};
0064     varargin(1:2) = [];
0065 end
0066 
0067 % format for displaying numeric values
0068 format = '%.2f';
0069 if ~isempty(varargin) 
0070     var1 = varargin{1};
0071     if ischar(var1) && var1(1) == '%'
0072         format = varargin{1};
0073         varargin(1) = [];
0074     end
0075 end
0076 if size(format, 1) == 1 && size(px, 1) > 1
0077     format = repmat(format, size(px, 1), 1);
0078 end
0079 
0080 % compute the strings that have to be displayed
0081 labels = cell(length(px), 1);
0082 if isnumeric(lbl)
0083     for i = 1:length(px)
0084         labels{i} = sprintf(format(i,:), lbl(i));
0085     end
0086 elseif ischar(lbl)
0087     for i = 1:length(px)
0088         labels{i} = lbl(i,:);
0089     end
0090 elseif iscell(lbl)
0091     labels = lbl;
0092 end
0093 labels = char(labels);
0094 
0095 % display the text
0096 h = text(px, py, labels, 'parent', ax, varargin{:});
0097 
0098 % format output
0099 if nargout > 0
0100     varargout = {h};
0101 end
0102

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019