Home > matGeom > geom2d > drawLabels.m

drawLabels

PURPOSE ^

DRAWLABELS Draw labels at specified positions.

SYNOPSIS ^

function varargout = drawLabels(varargin)

DESCRIPTION ^

DRAWLABELS 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 %DRAWLABELS 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 % E-mail: david.legland@inrae.fr
0025 % Created: 2003-12-15
0026 % Copyright 2003-2024 INRA - TPV URPOI - BIA IMASTE
0027 
0028 % check if enough inputs are given
0029 if isempty(varargin)
0030     error('wrong number of arguments in drawLabels');
0031 end
0032 
0033 % extract handle of axis to draw on
0034 if isAxisHandle(varargin{1})
0035     ax = varargin{1};
0036     varargin(1) = [];
0037 else
0038     ax = gca;
0039 end
0040 
0041 % process input parameters
0042 var = varargin{1};
0043 if size(var, 2) == 1
0044     % coordinates given as separate arguments
0045     if length(varargin) < 3
0046         error('wrong number of arguments in drawLabels');
0047     end
0048     px  = var;
0049     py  = varargin{2};
0050     lbl = varargin{3};
0051     varargin(1:3) = [];
0052 else
0053     % parameters given as a packed array
0054     if length(varargin) < 2
0055         error('wrong number of arguments in drawLabels');
0056     end
0057     px  = var(:,1);
0058     py  = var(:,2);
0059     lbl = varargin{2};
0060     varargin(1:2) = [];
0061 end
0062 
0063 % format for displaying numeric values
0064 format = '%.2f';
0065 if ~isempty(varargin) 
0066     var1 = varargin{1};
0067     if ischar(var1) && var1(1) == '%'
0068         format = varargin{1};
0069         varargin(1) = [];
0070     end
0071 end
0072 if size(format, 1) == 1 && size(px, 1) > 1
0073     format = repmat(format, size(px, 1), 1);
0074 end
0075 
0076 % compute the strings that have to be displayed
0077 labels = cell(length(px), 1);
0078 if isnumeric(lbl)
0079     for i = 1:length(px)
0080         labels{i} = sprintf(format(i,:), lbl(i));
0081     end
0082 elseif ischar(lbl)
0083     for i = 1:length(px)
0084         labels{i} = lbl(i,:);
0085     end
0086 elseif iscell(lbl)
0087     labels = lbl;
0088 end
0089 labels = char(labels);
0090 
0091 % display the text
0092 h = text(px, py, labels, 'parent', ax, varargin{:});
0093 
0094 % format output
0095 if nargout > 0
0096     varargout = {h};
0097 end
0098

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022