1## Copyright (C) 2021 David Legland
2## All rights reserved.
3##
4## Redistribution and use in source and binary forms, with or without
5## modification, are permitted provided that the following conditions are met:
6##
7##     1 Redistributions of source code must retain the above copyright notice,
8##       this list of conditions and the following disclaimer.
9##     2 Redistributions in binary form must reproduce the above copyright
10##       notice, this list of conditions and the following disclaimer in the
11##       documentation and/or other materials provided with the distribution.
12##
13## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
14## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
17## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23##
24## The views and conclusions contained in the software and documentation are
25## those of the authors and should not be interpreted as representing official
26## policies, either expressed or implied, of the copyright holders.
27
28function varargout = drawLabels(varargin)
29% Draw labels at specified positions.
30%
31%   drawLabels(X, Y, LBL)
32%   Draws labels LBL at positions given by X and Y.
33%   LBL can be either a string array, or a number array. In this case,
34%   string are created by using sprintf function, using the '%.2f' format.
35%
36%   drawLabels(POS, LBL)
37%   Draws labels LBL at position specified by POS, where POS is a N-by-2
38%   numeric array.
39%
40%   drawLabels(..., NUMBERS, FORMAT)
41%   Creates labels using sprintf function, with the mask given by FORMAT
42%   (e.g. '%03d' or '5.3f'), and the corresponding values.
43%
44%   drawLabels(..., PNAME, PVALUE)
45%   Specifies additional parameters to the created labels. See 'text'
46%   properties for available values.
47%
48
49%   ---------
50%   author : David Legland
51%   INRA - TPV URPOI - BIA IMASTE
52%   created the 15/12/2003.
53%
54
55%   HISTORY
56%   09/03/2007: (re)implement it...
57%   2011-10-11 add management of axes handle
58
59% check if enough inputs are given
60if isempty(varargin)
61    error('wrong number of arguments in drawLabels');
62end
63
64% extract handle of axis to draw on
65if isscalar(varargin{1}) && ishandle(varargin{1})
66    ax = varargin{1};
67    varargin(1) = [];
68else
69    ax = gca;
70end
71
72% process input parameters
73var = varargin{1};
74if size(var, 2) == 1
75    % coordinates given as separate arguments
76    if length(varargin) < 3
77        error('wrong number of arguments in drawLabels');
78    end
79    px  = var;
80    py  = varargin{2};
81    lbl = varargin{3};
82    varargin(1:3) = [];
83else
84    % parameters given as a packed array
85    if length(varargin) < 2
86        error('wrong number of arguments in drawLabels');
87    end
88    px  = var(:,1);
89    py  = var(:,2);
90    lbl = varargin{2};
91    varargin(1:2) = [];
92end
93
94% format for displaying numeric values
95format = '%.2f';
96if ~isempty(varargin)
97    var1 = varargin{1};
98    if ischar(var1) && var1(1) == '%'
99        format = varargin{1};
100        varargin(1) = [];
101    end
102end
103if size(format, 1) == 1 && size(px, 1) > 1
104    format = repmat(format, size(px, 1), 1);
105end
106
107% compute the strings that have to be displayed
108labels = cell(length(px), 1);
109if isnumeric(lbl)
110    for i = 1:length(px)
111        labels{i} = sprintf(format(i,:), lbl(i));
112    end
113elseif ischar(lbl)
114    for i = 1:length(px)
115        labels{i} = lbl(i,:);
116    end
117elseif iscell(lbl)
118    labels = lbl;
119end
120labels = char(labels);
121
122% display the text
123h = text(px, py, labels, 'parent', ax, varargin{:});
124
125% format output
126if nargout > 0
127    varargout = {h};
128end
129
130