comparison scripts/plot/appearance/text.m @ 17572:7bb76a22cde1

maint: Split scripts/plot directory into 4 pieces. scripts/gui : user-interface functions scripts/plot/appearance : functions controlling plot appearance scripts/plot/draw : plotting functions which produce graphs scripts/plot/util : low-level plotting functions and utilities. * scripts/gui/guidata.m, scripts/gui/guihandles.m, scripts/gui/module.mk, scripts/gui/private/__file_filter__.m, scripts/gui/private/__fltk_file_filter__.m, scripts/gui/private/__is_function__.m, scripts/gui/private/__uigetdir_fltk__.m, scripts/gui/private/__uigetfile_fltk__.m, scripts/gui/private/__uiobject_split_args__.m, scripts/gui/private/__uiputfile_fltk__.m, scripts/gui/uicontextmenu.m, scripts/gui/uicontrol.m, scripts/gui/uigetdir.m, scripts/gui/uigetfile.m, scripts/gui/uimenu.m, scripts/gui/uipanel.m, scripts/gui/uipushtool.m, scripts/gui/uiputfile.m, scripts/gui/uiresume.m, scripts/gui/uitoggletool.m, scripts/gui/uitoolbar.m, scripts/gui/uiwait.m, scripts/gui/waitbar.m, scripts/gui/waitforbuttonpress.m: Moved from scripts/plot to scripts/gui * scripts/plot/appearance/__clabel__.m, scripts/plot/appearance/__getlegenddata__.m, scripts/plot/appearance/axis.m, scripts/plot/appearance/box.m, scripts/plot/appearance/caxis.m, scripts/plot/appearance/clabel.m, scripts/plot/appearance/daspect.m, scripts/plot/appearance/diffuse.m, scripts/plot/appearance/grid.m, scripts/plot/appearance/gtext.m, scripts/plot/appearance/hidden.m, scripts/plot/appearance/legend.m, scripts/plot/appearance/orient.m, scripts/plot/appearance/pbaspect.m, scripts/plot/appearance/private/__axis_label__.m, scripts/plot/appearance/private/__axis_limits__.m, scripts/plot/appearance/shading.m, scripts/plot/appearance/specular.m, scripts/plot/appearance/text.m, scripts/plot/appearance/title.m, scripts/plot/appearance/view.m, scripts/plot/appearance/whitebg.m, scripts/plot/appearance/xlabel.m, scripts/plot/appearance/xlim.m, scripts/plot/appearance/ylabel.m, scripts/plot/appearance/ylim.m, scripts/plot/appearance/zlabel.m, scripts/plot/appearance/zlim.m: Moved from scripts/plot to subdir appearance. * scripts/plot/draw/area.m, scripts/plot/draw/bar.m, scripts/plot/draw/barh.m, scripts/plot/draw/colorbar.m, scripts/plot/draw/comet.m, scripts/plot/draw/comet3.m, scripts/plot/draw/compass.m, scripts/plot/draw/contour.m, scripts/plot/draw/contour3.m, scripts/plot/draw/contourc.m, scripts/plot/draw/contourf.m, scripts/plot/draw/cylinder.m, scripts/plot/draw/ellipsoid.m, scripts/plot/draw/errorbar.m, scripts/plot/draw/ezcontour.m, scripts/plot/draw/ezcontourf.m, scripts/plot/draw/ezmesh.m, scripts/plot/draw/ezmeshc.m, scripts/plot/draw/ezplot.m, scripts/plot/draw/ezplot3.m, scripts/plot/draw/ezpolar.m, scripts/plot/draw/ezsurf.m, scripts/plot/draw/ezsurfc.m, scripts/plot/draw/feather.m, scripts/plot/draw/fill.m, scripts/plot/draw/fplot.m, scripts/plot/draw/hist.m, scripts/plot/draw/isocolors.m, scripts/plot/draw/isonormals.m, scripts/plot/draw/isosurface.m, scripts/plot/draw/line.m, scripts/plot/draw/loglog.m, scripts/plot/draw/loglogerr.m, scripts/plot/draw/mesh.m, scripts/plot/draw/meshc.m, scripts/plot/draw/meshz.m, scripts/plot/draw/pareto.m, scripts/plot/draw/patch.m, scripts/plot/draw/pcolor.m, scripts/plot/draw/peaks.m, scripts/plot/draw/pie.m, scripts/plot/draw/pie3.m, scripts/plot/draw/plot.m, scripts/plot/draw/plot3.m, scripts/plot/draw/plotmatrix.m, scripts/plot/draw/plotyy.m, scripts/plot/draw/polar.m, scripts/plot/draw/private/__add_datasource__.m, scripts/plot/draw/private/__bar__.m, scripts/plot/draw/private/__contour__.m, scripts/plot/draw/private/__errcomm__.m, scripts/plot/draw/private/__errplot__.m, scripts/plot/draw/private/__ezplot__.m, scripts/plot/draw/private/__interp_cube__.m, scripts/plot/draw/private/__line__.m, scripts/plot/draw/private/__marching_cube__.m, scripts/plot/draw/private/__patch__.m, scripts/plot/draw/private/__pie__.m, scripts/plot/draw/private/__plt__.m, scripts/plot/draw/private/__quiver__.m, scripts/plot/draw/private/__scatter__.m, scripts/plot/draw/private/__stem__.m, scripts/plot/draw/quiver.m, scripts/plot/draw/quiver3.m, scripts/plot/draw/rectangle.m, scripts/plot/draw/ribbon.m, scripts/plot/draw/rose.m, scripts/plot/draw/scatter.m, scripts/plot/draw/scatter3.m, scripts/plot/draw/semilogx.m, scripts/plot/draw/semilogxerr.m, scripts/plot/draw/semilogy.m, scripts/plot/draw/semilogyerr.m, scripts/plot/draw/shrinkfaces.m, scripts/plot/draw/slice.m, scripts/plot/draw/sombrero.m, scripts/plot/draw/sphere.m, scripts/plot/draw/stairs.m, scripts/plot/draw/stem.m, scripts/plot/draw/stem3.m, scripts/plot/draw/stemleaf.m, scripts/plot/draw/surf.m, scripts/plot/draw/surface.m, scripts/plot/draw/surfc.m, scripts/plot/draw/surfl.m, scripts/plot/draw/surfnorm.m, scripts/plot/draw/tetramesh.m, scripts/plot/draw/trimesh.m, scripts/plot/draw/triplot.m, scripts/plot/draw/trisurf.m, scripts/plot/draw/waterfall.m: Moved from plot/ to subdir draw. * scripts/plot/util/__actual_axis_position__.m, scripts/plot/util/__default_plot_options__.m, scripts/plot/util/__gnuplot_drawnow__.m, scripts/plot/util/__next_line_color__.m, scripts/plot/util/__next_line_style__.m, scripts/plot/util/__plt_get_axis_arg__.m, scripts/plot/util/__pltopt__.m, scripts/plot/util/allchild.m, scripts/plot/util/ancestor.m, scripts/plot/util/axes.m, scripts/plot/util/cla.m, scripts/plot/util/clf.m, scripts/plot/util/close.m, scripts/plot/util/closereq.m, scripts/plot/util/colstyle.m, scripts/plot/util/copyobj.m, scripts/plot/util/figure.m, scripts/plot/util/findall.m, scripts/plot/util/findfigs.m, scripts/plot/util/findobj.m, scripts/plot/util/gca.m, scripts/plot/util/gcbf.m, scripts/plot/util/gcbo.m, scripts/plot/util/gcf.m, scripts/plot/util/gco.m, scripts/plot/util/ginput.m, scripts/plot/util/gnuplot_binary.in, scripts/plot/util/graphics_toolkit.m, scripts/plot/util/hdl2struct.m, scripts/plot/util/hggroup.m, scripts/plot/util/hold.m, scripts/plot/util/isaxes.m, scripts/plot/util/isfigure.m, scripts/plot/util/ishghandle.m, scripts/plot/util/ishold.m, scripts/plot/util/isprop.m, scripts/plot/util/linkprop.m, scripts/plot/util/meshgrid.m, scripts/plot/util/ndgrid.m, scripts/plot/util/newplot.m, scripts/plot/util/print.m, scripts/plot/util/printd.m, scripts/plot/util/private/__add_default_menu__.m, scripts/plot/util/private/__fltk_ginput__.m, scripts/plot/util/private/__fltk_print__.m, scripts/plot/util/private/__ghostscript__.m, scripts/plot/util/private/__gnuplot_get_var__.m, scripts/plot/util/private/__gnuplot_ginput__.m, scripts/plot/util/private/__gnuplot_has_feature__.m, scripts/plot/util/private/__gnuplot_has_terminal__.m, scripts/plot/util/private/__gnuplot_open_stream__.m, scripts/plot/util/private/__gnuplot_print__.m, scripts/plot/util/private/__gnuplot_version__.m, scripts/plot/util/private/__go_draw_axes__.m, scripts/plot/util/private/__go_draw_figure__.m, scripts/plot/util/private/__print_parse_opts__.m, scripts/plot/util/private/__tight_eps_bbox__.m, scripts/plot/util/refresh.m, scripts/plot/util/refreshdata.m, scripts/plot/util/saveas.m, scripts/plot/util/shg.m, scripts/plot/util/struct2hdl.m, scripts/plot/util/subplot.m: Moved from plot to subdir util. * etc/HACKING: Updated directory structure info. * scripts/Makefile.am, scripts/plot/appearance/module.mk, scripts/plot/draw/module.mk, scripts/plot/util/module.mk: Added new directories to build system.
author Rik <rik@octave.org>
date Fri, 04 Oct 2013 17:09:08 -0700
parents scripts/plot/text.m@4166048ba6cf
children d63878346099
comparison
equal deleted inserted replaced
17571:6e4ea5c8a4bb 17572:7bb76a22cde1
1 ## Copyright (C) 2007-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} text (@var{x}, @var{y}, @var{string})
21 ## @deftypefnx {Function File} {} text (@var{x}, @var{y}, @var{z}, @var{string})
22 ## @deftypefnx {Function File} {} text (@dots{}, @var{prop}, @var{val}, @dots{})
23 ## @deftypefnx {Function File} {@var{h} =} text (@dots{})
24 ## Create a text object with text @var{string} at position @var{x}, @var{y},
25 ## (@var{z}) on the current axes.
26 ##
27 ## Multiple locations can be specified if @var{x}, @var{y}, (@var{z}) are
28 ## vectors. Multiple strings can be specified with a character matrix or
29 ## a cell array of strings.
30 ##
31 ## Optional property/value pairs may be used to control the appearance of the
32 ## text.
33 ##
34 ## The optional return value @var{h} is a vector of graphics handles to the
35 ## created text objects.
36 ## @seealso{gtext, title, xlabel, ylabel, zlabel}
37 ## @end deftypefn
38
39 ## Author: jwe
40
41 ## Note: The following code is rigged for Matlab compatibility and is
42 ## full of hidden assumptions. Be very wary when modifying.
43
44 function h = text (varargin)
45
46 nargs = nargin;
47 offset = 0;
48
49 if (nargs > 2 && isnumeric (varargin{1}) && isnumeric (varargin{2}))
50 x = varargin{1};
51 y = varargin{2};
52
53 if (nargin > 3 && isnumeric (varargin{3}))
54 z = varargin{3};
55 offset = 4;
56 else
57 z = zeros (size (x));
58 offset = 3;
59 endif
60
61 string = varargin{offset};
62 varargin(1:offset) = [];
63
64 nx = numel (x);
65 ny = numel (y);
66 nz = numel (z);
67 if (ischar (string))
68
69 do_keyword_repl = true;
70 nt = rows (string);
71 if (nx == 1 && nt == 1)
72 ## Single text object with one line
73 string = {string};
74 elseif (nx == 1 && nt > 1)
75 ## Single text object with multiple lines
76 ## FIXME: "default" or "factory" as first row
77 ## should be escaped to "\default" or "\factory"
78 ## Other rows do not require escaping.
79 do_keyword_repl = false;
80 string = {string};
81 elseif (nx > 1 && nt == nx)
82 ## Mutiple text objects with different strings
83 string = cellstr (string);
84 else
85 ## Mutiple text objects with same string
86 string = repmat ({string}, [nx, 1]);
87 nt = nx;
88 endif
89
90 ## Escape special keywords
91 if (do_keyword_repl)
92 string = regexprep (string, '^(default|factory)$', '\\$1');
93 endif
94
95 elseif (iscell (string))
96
97 nt = numel (string);
98 if (nx == 1)
99 ## Single text object with one or more lines
100 string = {string};
101 nt = 1;
102 elseif (nx > 1 && nt == nx)
103 ## Mutiple text objects with different strings
104 else
105 ## Mutiple text objects with same string
106 string = repmat ({string}, [nx, 1]);
107 nt = nx;
108 endif
109
110 else
111
112 error ("text: STRING must be a character string or cell array of character strings");
113
114 endif
115 else # Only PROP/VALUE pairs
116 x = y = z = 0;
117 nx = ny = nz = 1;
118 string = {""};
119 nt = 1;
120 endif
121
122 ## Any remaining inputs must occur as PROPERTY/VALUE pairs
123 if (rem (numel (varargin), 2) != 0)
124 print_usage ();
125 endif
126
127 ## Get axis argument which may be in a 'parent' PROP/VAL pair
128 [hax, varargin] = __plt_get_axis_arg__ ("text", varargin{:});
129
130 if (isempty (hax))
131 hax = gca ();
132 endif
133
134 ## Position argument may alse be in PROP/VAL pair
135 idx = find (strcmpi (varargin, "position"), 1);
136 if (idx)
137 pos = varargin{idx+1};
138 varargin(idx:idx+1) = [];
139 else
140 pos = [x(:), y(:), z(:)];
141 endif
142
143 if (nx == ny && nx == nz && (nt == nx || nt == 1 || nx == 1))
144 htmp = zeros (nt, 1);
145 if (nx == 1)
146 htmp = __go_text__ (hax, "string", string{1},
147 ## varargin first, in case "Units" set for pos.
148 varargin{:},
149 "position", pos);
150 elseif (nx == nt)
151 for n = 1:nt
152 htmp(n) = __go_text__ (hax, "string", string{n},
153 varargin{:},
154 "position", pos(n,:));
155 endfor
156 __request_drawnow__ ();
157 else
158 error ("text: dimension mismatch for coordinates and STRING");
159 endif
160 elseif (nt == nx || nt == 1 || nx == 1)
161 error ("text: dimension mismatch for coordinates");
162 else
163 error ("text: dimension mismatch between coordinates and strings");
164 endif
165
166 if (nargout > 0)
167 h = htmp;
168 endif
169
170 endfunction
171
172
173 %!demo
174 %! clf;
175 %! ha = {'left', 'center', 'right'};
176 %! va = {'bottom', 'middle', 'top'};
177 %! x = [0.25 0.5 0.75];
178 %! y = x;
179 %! for t = 0:30:359;
180 %! for nh = 1:numel (ha)
181 %! for nv = 1:numel (va)
182 %! text (x(nh), y(nv), 'Hello World', ...
183 %! 'rotation', t, ...
184 %! 'horizontalalignment', ha{nh}, ...
185 %! 'verticalalignment', va{nv});
186 %! end
187 %! end
188 %! end
189 %! set (gca, 'xtick', [0.25, 0.5, 0.75], ...
190 %! 'xticklabel', ha, ...
191 %! 'ytick', [0.25, 0.5, 0.75], ...
192 %! 'yticklabel', va);
193 %! axis ([0 1 0 1]);
194 %! xlabel ('horizontal alignment');
195 %! ylabel ('vertical alignment');
196 %! title ('text alignment and rotation (0:30:360 degrees)');
197
198 %!demo
199 %! clf;
200 %! h = mesh (peaks, 'edgecolor', 0.7 * [1 1 1], ...
201 %! 'facecolor', 'none', ...
202 %! 'facealpha', 0);
203 %! for t = 0:45:359;
204 %! text (25, 25, 0, 'Vertical Alignment = Bottom', ...
205 %! 'rotation', t, ...
206 %! 'horizontalalignment', 'left', ...
207 %! 'verticalalignment', 'bottom');
208 %! end
209 %! caxis ([-100 100]);
210 %! title ('Vertically Aligned at Bottom');
211
212 %!demo
213 %! clf;
214 %! axis ([0 8 0 8]);
215 %! title (['1st title';'2nd title']);
216 %! xlabel (['1st xlabel';'2nd xlabel']);
217 %! ylabel (['1st ylabel';'2nd ylabel']);
218 %! text (4, 4, {'Hello', 'World'}, ...
219 %! 'horizontalalignment', 'center', ...
220 %! 'verticalalignment', 'middle');
221 %! grid on;
222
223 %!demo
224 %! clf;
225 %! h = mesh (peaks (), 'edgecolor', 0.7 * [1 1 1], ...
226 %! 'facecolor', 'none', ...
227 %! 'facealpha', 0);
228 %! title (['1st title';'2nd title']);
229 %! xlabel (['1st xlabel';'2nd xlabel']);
230 %! ylabel (['1st ylabel';'2nd ylabel']);
231 %! zlabel (['1st zlabel';'2nd zlabel']);
232 %! text (0, 0, 5, {'Hello', 'World'}, ...
233 %! 'horizontalalignment', 'center', ...
234 %! 'verticalalignment', 'middle');
235 %! hold on;
236 %! plot3 (0, 0, 5, '+k');
237
238 %!demo
239 %! clf;
240 %! h = text (0.5, 0.3, 'char');
241 %! assert ('char', class (get (h, 'string')));
242 %! h = text (0.5, 0.4, ['char row 1'; 'char row 2']);
243 %! assert ('char', class (get (h, 'string')));
244 %! h = text (0.5, 0.6, {'cell2str (1,1)', 'cell2str (1,2)'; 'cell2str (2,1)', 'cell2str (2,2)'});
245 %! assert ('cell', class (get (h, 'string')));
246 %! h = text (0.5, 0.8, 'foobar');
247 %! set (h, 'string', 1:3);
248 %! h = text ([0.1, 0.1], [0.3, 0.4], 'one string & two objects');
249 %! assert ('char', class (get (h(1), 'string')));
250 %! assert ('char', class (get (h(2), 'string')));
251 %! h = text ([0.1, 0.1], [0.5, 0.6], {'one cellstr & two objects'});
252 %! assert ('cell', class (get (h(1), 'string')));
253 %! assert ('cell', class (get (h(2), 'string')));
254 %! h = text ([0.1, 0.1], [0.7, 0.8], {'cellstr 1 object 1', 'cellstr 2 object 2'});
255 %! assert ('char', class (get (h(1), 'string')));
256 %! assert ('char', class (get (h(2), 'string')));
257 %! h = text ([0.1, 0.1], [0.1, 0.2], ['1st string & 1st object'; '2nd string & 2nd object']);
258 %! assert ('char', class (get (h(1), 'string')));
259 %! assert ('char', class (get (h(2), 'string')));
260 %! h = text (0.7, 0.6, 'single string');
261 %! assert ('char', class (get (h, 'string')));
262 %! h = text (0.7, 0.5, {'single cell-string'});
263 %! assert ('cell', class (get (h, 'string')));
264 %! xlabel (1:2);
265 %! ylabel (1:2);
266 %! title (1:2);
267
268 %!test
269 %! hf = figure ("visible", "off");
270 %! unwind_protect
271 %! ## Single object with one line
272 %! h = text (0.5, 0.3, "single object with one line");
273 %! obs = get (h, "string");
274 %! assert (class (obs), "char");
275 %! assert (obs, "single object with one line");
276 %!
277 %! ## Single object with multiple lines
278 %! h = text (0.5, 0.4, ["char row 1"; "char row 2"]);
279 %! obs = get (h, "string");
280 %! assert (class (obs), "char");
281 %! assert (obs, ["char row 1"; "char row 2"]);
282 %!
283 %! ## Multiple objects with single line
284 %! h = text ([0.1, 0.1], [0.3, 0.4], "two objects with same string");
285 %! assert (class (get (h(1), "string")), "char");
286 %! assert (class (get (h(2), "string")), "char");
287 %! assert (get (h(1), "string"), "two objects with same string");
288 %! assert (get (h(2), "string"), "two objects with same string");
289 %!
290 %! ## Multiple objects with multiple lines
291 %! h = text ([0.1, 0.1], [0.3, 0.4], ["string1"; "string2"]);
292 %! assert (class (get (h(1), "string")), "char");
293 %! assert (class (get (h(2), "string")), "char");
294 %! assert (get (h(1), "string"), "string1");
295 %! assert (get (h(2), "string"), "string2");
296 %!
297 %! ### Tests repeated with cell input ###
298 %!
299 %! ## Single object with one line
300 %! h = text (0.5, 0.3, {"single object with one line"});
301 %! obs = get (h, "string");
302 %! assert (class (obs), "cell");
303 %! assert (obs, {"single object with one line"});
304 %!
305 %! ## Single object with multiple lines
306 %! h = text (0.5, 0.6, {"cell2str (1,1)", "cell2str (1,2)";
307 %! "cell2str (2,1)", "cell2str (2,2)"});
308 %! obs = get (h, "string");
309 %! assert (class (obs), "cell");
310 %! assert (obs, {"cell2str (1,1)"; "cell2str (2,1)";
311 %! "cell2str (1,2)"; "cell2str (2,2)"});
312 %!
313 %! ## Multiple objects with single line
314 %! h = text ([0.1, 0.1], [0.5, 0.6], {"two objects with same cellstr"});
315 %! assert (class (get (h(1), "string")), "cell");
316 %! assert (class (get (h(2), "string")), "cell");
317 %! ## FIXME: is return value of cellstr, rather than string, Matlab-verified?
318 %! assert (get (h(1), "string"), {"two objects with same cellstr"});
319 %! assert (get (h(2), "string"), {"two objects with same cellstr"});
320 %!
321 %! ## Multiple objects with multiple lines
322 %! h = text ([0.1, 0.1], [0.7, 0.8], {"cellstr1", "cellstr2"});
323 %! ## FIXME: is return value really char in Matlab?
324 %! assert (class (get (h(1), "string")), "char");
325 %! assert (class (get (h(2), "string")), "char");
326 %! assert (get (h(1), "string"), "cellstr1");
327 %! assert (get (h(2), "string"), "cellstr2");
328 %!
329 %! ## Test special keyword processing
330 %! h = text (0.5, 0.5, "default");
331 %! assert (get (h, "string"), "default")
332 %! h = text (0.5, 0.5, "factory");
333 %! assert (get (h, "string"), "factory")
334 %!
335 %! unwind_protect_cleanup
336 %! close (hf);
337 %! end_unwind_protect
338