-
Notifications
You must be signed in to change notification settings - Fork 714
/
ft_plot_text.m
143 lines (125 loc) · 5.17 KB
/
ft_plot_text.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function [varargout] = ft_plot_text(X, Y, str, varargin)
% FT_PLOT_TEXT helper function for plotting text, which can also be used in
% combination with the multiple channel layout display in FieldTrip.
%
% Use as
% ft_plot_text(X, Y, str, ...)
%
% Optional arguments should come in key-value pairs and can include
% 'fontcolor' = string, color specification (default = 'k')
% 'fontsize' = number, sets the size of the text (default = 10)
% 'fontunits' =
% 'fontname' =
% 'fontweight' =
% 'horizontalalignment' =
% 'verticalalignment' =
% 'interpreter' = string, can be 'none', 'tex' or 'latex' (default = 'none')
% 'rotation' =
% 'tag' = string, the tag assigned to the plotted elements (default = '')
%
% It is possible to plot the object in a local pseudo-axis (c.f. subplot), which is specfied as follows
% 'hpos' = horizontal position of the center of the local axes
% 'vpos' = vertical position of the center of the local axes
% 'width' = width of the local axes
% 'height' = height of the local axes
% 'hlim' = horizontal scaling limits within the local axes
% 'vlim' = vertical scaling limits within the local axes
%
% Example
% figure
% ft_plot_vector(randn(1,10), rand(1,10), 'hpos', 1, 'vpos', 1, 'width', 0.2, 'height', 0.2, 'box', true)
% ft_plot_text(0, 0 , '+', 'hpos', 1, 'vpos', 1, 'width', 0.2, 'height', 0.2)
% axis([0 2 0 2])
%
% See also FT_PLOT_VECTOR, FT_PLOT_MATRIX, FT_PLOT_LINE, FT_PLOT_BOX
% Copyrights (C) 2009-2022, Robert Oostenveld
%
% This file is part of FieldTrip, see http:https://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http:https://www.gnu.org/licenses/>.
%
% $Id$
% get the optional input arguments
hpos = ft_getopt(varargin, 'hpos');
vpos = ft_getopt(varargin, 'vpos');
width = ft_getopt(varargin, 'width');
height = ft_getopt(varargin, 'height');
hlim = ft_getopt(varargin, 'hlim');
vlim = ft_getopt(varargin, 'vlim');
tag = ft_getopt(varargin, 'tag', '');
% these have to do with the font
color = ft_getopt(varargin, 'fontcolor', 'k');
fontsize = ft_getopt(varargin, 'fontsize', get(0, 'defaulttextfontsize'));
fontname = ft_getopt(varargin, 'fontname', get(0, 'defaulttextfontname'));
fontweight = ft_getopt(varargin, 'fontweight', get(0, 'defaulttextfontweight'));
fontunits = ft_getopt(varargin, 'fontunits', get(0, 'defaulttextfontunits'));
% these also have to do with the font
horizontalalignment = ft_getopt(varargin, 'horizontalalignment', 'center');
verticalalignment = ft_getopt(varargin, 'verticalalignment', 'middle');
rotation = ft_getopt(varargin, 'rotation', 0);
interpreter = ft_getopt(varargin, 'interpreter', 'none');
% color management
if ischar(color) && exist([color '.m'], 'file')
color = eval(color);
elseif ischar(color) && ismember(color, htmlcolors)
color = htmlcolors(color);
end
if isempty(hlim) && isempty(vlim) && isempty(hpos) && isempty(vpos) && isempty(height) && isempty(width)
% no scaling is needed, the input X and Y are already fine
% use a shortcut to speed up the plotting
else
% use the full implementation
if isempty(hlim)
ft_warning('use hlim/vlim when specifying local axes');
hlim = get(gca, 'XLim');
end
if isempty(vlim)
ft_warning('use hlim/vlim when specifying local axes');
vlim = get(gca, 'YLim');
end
if isempty(hpos)
hpos = (hlim(1)+hlim(2))/2;
end
if isempty(vpos)
vpos = (vlim(1)+vlim(2))/2;
end
if isempty(width)
width = hlim(2)-hlim(1);
end
if isempty(height)
height = vlim(2)-vlim(1);
end
X = X - hlim(1);
X = X ./ (hlim(2)-hlim(1));
X = X .* width;
X = X + hpos - width/2;
Y = Y - vlim(1);
Y = Y ./ (vlim(2)-vlim(1));
Y = Y .* height;
Y = Y + vpos - height/2;
end % shortcut
% it fails on single inputs
X = double(X);
Y = double(Y);
h = text(X, Y, str, 'color', color, 'fontunits', fontunits, 'fontsize', fontsize, 'fontname', fontname, 'fontweight', fontweight);
if ~isempty(horizontalalignment), set(h, 'horizontalalignment', horizontalalignment); end
if ~isempty(verticalalignment), set(h, 'verticalalignment', verticalalignment); end
if ~isempty(rotation), set(h, 'rotation', rotation); end
set(h, 'tag', tag);
set(h, 'interpreter', interpreter);
% the (optional) output is the handle
if nargout == 1
varargout{1} = h;
end