]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/private/__plt__.m
update packages
[CreaPhase.git] / octave_packages / m / plot / private / __plt__.m
1 ## Copyright (C) 1993-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} {} __plt__ (@var{caller}, @var{h}, @var{varargin})
21 ## Undocumented internal function.
22 ## @end deftypefn
23
24 ## Author: jwe
25
26 function retval = __plt__ (caller, h, varargin)
27
28   nargs = nargin - 2;
29
30   if (nargs > 0)
31
32     k = 1;
33
34     x_set = false;
35     y_set = false;
36     property_set = false;
37     properties = {};
38
39     hlegend = [];
40     fkids = get (gcf(), "children");
41     for i = 1 : numel(fkids)
42       if (ishandle (fkids (i)) && strcmp (get (fkids (i), "type"), "axes")
43           && (strcmp (get (fkids (i), "tag"), "legend")))
44         udata = get (fkids (i), "userdata");
45         if (! isempty (intersect (udata.handle, gca ())))
46           hlegend = fkids (i);
47           break;
48         endif
49       endif
50     endfor
51
52     setlgnd = false;
53     if (isempty (hlegend))
54       hlgnd = [];
55       tlgnd = {};
56     else
57       [hlgnd, tlgnd] = __getlegenddata__ (hlegend);
58     endif
59
60     ## Gather arguments, decode format, gather plot strings, and plot lines.
61
62     retval = [];
63
64     while (nargs > 0 || x_set)
65
66       if (nargs == 0)
67         ## Force the last plot when input variables run out.
68         next_cell = {};
69         next_arg = {""};
70       else
71         next_cell = varargin(k);
72         next_arg = varargin{k++};
73       endif
74
75       nargs--;
76
77       if (ischar (next_arg) || iscellstr (next_arg))
78         if (x_set)
79           [options, valid] = __pltopt__ (caller, next_arg, false);
80           if (! valid)
81             if (nargs == 0)
82               error ("%s: properties must appear followed by a value", caller);
83             endif
84             properties = [properties, [next_cell, varargin(k++)]];
85             nargs--;
86             continue;
87           else
88             while (nargs > 0 && ischar (varargin{k}))
89               if (nargs < 2)
90                 error ("%s: properties must appear followed by a value",
91                        caller);
92               endif
93               properties = [properties, varargin(k:k+1)];
94               k += 2;
95               nargs -= 2;
96             endwhile
97           endif
98           if (y_set)
99             tmp = __plt2__ (h, x, y, options, properties);
100             [hlgnd, tlgnd, setlgnd] = __plt_key__ (tmp, options, hlgnd, tlgnd, setlgnd);
101             properties = {};
102             retval = [retval; tmp];
103           else
104             tmp = __plt1__ (h, x, options, properties);
105             [hlgnd, tlgnd, setlgnd] = __plt_key__ (tmp, options, hlgnd, tlgnd, setlgnd);
106             properties = {};
107             retval = [retval; tmp];
108           endif
109           x_set = false;
110           y_set = false;
111         else
112           error ("plot: no data to plot");
113         endif
114       elseif (x_set)
115         if (y_set)
116           options = __pltopt__ (caller, {""});
117           tmp = __plt2__ (h, x, y, options, properties);
118           [hlgnd, tlgnd, setlgnd] = __plt_key__ (tmp, options, hlgnd, tlgnd, setlgnd);
119           retval = [retval; tmp];
120           x = next_arg;
121           y_set = false;
122           properties = {};
123         else
124           y = next_arg;
125           y_set = true;
126         endif
127       else
128         x = next_arg;
129         x_set = true;
130       endif
131
132     endwhile
133
134     if (setlgnd)
135       legend (gca(), hlgnd, tlgnd);
136     endif
137   else
138     error ("__plt__: invalid number of arguments");
139   endif
140
141 endfunction
142
143 function [hlgnd, tlgnd, setlgnd] = __plt_key__ (h, options, hlgnd, tlgnd, setlgnd)
144   n = numel (h);
145   if (numel (options) == 1)
146     options = repmat (options(:), n, 1);
147   endif
148
149   for i = 1 : n
150     key = options.key;
151     if (! isempty (key))
152       hlgnd = [hlgnd(:); h(i)];
153       tlgnd = {tlgnd{:}, key};
154       setlgnd = true;
155     endif
156   endfor
157 endfunction
158
159 function retval = __plt1__ (h, x1, options, properties)
160
161   if (nargin < 2 || nargin > 4)
162     print_usage ();
163   endif
164
165   if (nargin < 3 || isempty (options))
166     options = __default_plot_options__ ();
167   endif
168
169   if (nargin < 4)
170     properties = {};
171   endif
172
173   if (! isstruct (options))
174     error ("__plt1__: options must be a struct array");
175   endif
176
177   [nr, nc] = size (x1);
178   if (nr == 1)
179     x1 = x1.';
180     tmp = nr;
181     nr = nc;
182     nc = tmp;
183   endif
184   x1_i = imag (x1);
185   if (any (any (x1_i)))
186     x2 = x1_i;
187     x1 = real (x1);
188   else
189     x2 = x1;
190     x1 = (1:nr)';
191   endif
192
193   retval = __plt2__ (h, x1, x2, options, properties);
194
195 endfunction
196
197 function retval = __plt2__ (h, x1, x2, options, properties)
198
199   if (nargin < 3 || nargin > 5)
200     print_usage ();
201   endif
202
203   if (nargin < 4 || isempty (options))
204     options = __default_plot_options__ ();
205   endif
206
207   if (nargin < 5)
208     properties = {};
209   endif
210
211   if (! isstruct (options))
212     error ("__plt1__: options must be a struct array");
213   endif
214
215   if (any (any (imag (x1))))
216     x1 = real (x1);
217   endif
218
219   if (any (any (imag (x2))))
220     x2 = real (x2);
221   endif
222
223   h_set = false;
224   if (isempty (x1) && isempty (x2))
225     retval = zeros (0, 1);
226   elseif (isscalar (x1))
227     if (isscalar (x2))
228       retval = __plt2ss__ (h, x1, x2, options, properties);
229     elseif (isvector (x2))
230       retval = __plt2sv__ (h, x1, x2, options, properties);
231     else
232       error ("__plt2__: invalid data for plotting");
233     endif
234   elseif (isvector (x1))
235     if (isscalar (x2))
236       retval = __plt2vs__ (h, x1, x2, options, properties);
237     elseif (isvector (x2))
238       retval = __plt2vv__ (h, x1, x2, options, properties);
239     elseif (ismatrix (x2))
240       retval = __plt2vm__ (h, x1, x2, options, properties);
241     else
242       error ("__plt2__: invalid data for plotting");
243     endif
244   elseif (ismatrix (x1))
245     if (isvector (x2))
246       retval = __plt2mv__ (h, x1, x2, options, properties);
247     elseif (ismatrix (x2))
248       retval = __plt2mm__ (h, x1, x2, options, properties);
249     else
250       error ("__plt2__: invalid data for plotting");
251     endif
252   else
253     error ("__plt2__: invalid data for plotting");
254   endif
255
256 endfunction
257
258 function retval = __plt2mm__ (h, x, y, options, properties)
259
260   if (nargin < 3 || nargin > 5)
261     print_usage ();
262   endif
263
264   if (nargin < 4 || isempty (options))
265     options = __default_plot_options__ ();
266   endif
267
268   if (nargin < 5)
269     properties = {};
270   endif
271
272   [x_nr, x_nc] = size (x);
273   [y_nr, y_nc] = size (y);
274
275   k = 1;
276   if (x_nr == y_nr && x_nc == y_nc)
277     if (x_nc > 0)
278       if (numel (options) == 1)
279         options = repmat (options(:), x_nc, 1);
280       endif
281       retval = zeros (x_nc, 1);
282       for i = 1:x_nc
283         linestyle = options(i).linestyle;
284         marker = options(i).marker;
285         if (isempty (marker) && isempty (linestyle))
286            [linestyle, marker] = __next_line_style__ ();
287         endif
288         color = options(i).color;
289         if (isempty (color))
290           color = __next_line_color__ ();
291         endif
292
293         retval(i) = line (x(:,i), y(:,i), "color", color,
294                           "linestyle", linestyle,
295                           "marker", marker, properties{:});
296       endfor
297     else
298       error ("__plt2mm__: arguments must be a matrices");
299     endif
300   else
301     error ("__plt2mm__: matrix dimensions must match");
302   endif
303
304 endfunction
305
306 function retval = __plt2mv__ (h, x, y, options, properties)
307
308   if (nargin < 3 || nargin > 5)
309     print_usage ();
310   endif
311
312   if (nargin < 4 || isempty (options))
313     options = __default_plot_options__ ();
314   endif
315
316   if (nargin < 5)
317     properties = {};
318   endif
319
320   [x_nr, x_nc] = size (x);
321   [y_nr, y_nc] = size (y);
322
323   if (y_nr == 1)
324     y = y';
325     tmp = y_nr;
326     y_nr = y_nc;
327     y_nc = tmp;
328   endif
329
330   if (x_nr == y_nr)
331     1;
332   elseif (x_nc == y_nr)
333     x = x';
334     tmp = x_nr;
335     x_nr = x_nc;
336     x_nc = tmp;
337   else
338     error ("__plt2mv__: matrix dimensions must match");
339   endif
340
341   if (x_nc > 0)
342     if (numel (options) == 1)
343       options = repmat (options(:), x_nc, 1);
344     endif
345     retval = zeros (x_nc, 1);
346     for i = 1:x_nc
347       linestyle = options(i).linestyle;
348       marker = options(i).marker;
349       if (isempty (marker) && isempty (linestyle))
350         [linestyle, marker] = __next_line_style__ ();
351       endif
352       color = options(i).color;
353       if (isempty (color))
354         color = __next_line_color__ ();
355       endif
356
357       retval(i) = line (x(:,i), y, "color", color,
358                         "linestyle", linestyle,
359                         "marker", marker, properties{:});
360     endfor
361   else
362     error ("__plt2mv__: arguments must be a matrices");
363   endif
364
365 endfunction
366
367 function retval = __plt2ss__ (h, x, y, options, properties)
368
369   if (nargin < 3 || nargin > 5)
370     print_usage ();
371   endif
372
373   if (nargin < 4 || isempty (options))
374     options = __default_plot_options__ ();
375   endif
376
377   if (nargin < 5)
378     properties = {};
379   endif
380
381   if (numel (options) > 1)
382     options = options(1);
383   endif
384
385   [x_nr, x_nc] = size (x);
386   [y_nr, y_nc] = size (y);
387
388   if (x_nr == 1 && x_nr == y_nr && x_nc == 1 && x_nc == y_nc)
389     linestyle = options.linestyle;
390     marker = options.marker;
391     if (isempty (marker) && isempty (linestyle))
392       [linestyle, marker] = __next_line_style__ ();
393     endif
394     color = options.color;
395     if (isempty (color))
396       color = __next_line_color__ ();
397     endif
398
399     retval = line (x, y, "color", color,
400                    "linestyle", linestyle,
401                    "marker", marker, properties{:});
402   else
403     error ("__plt2ss__: arguments must be scalars");
404   endif
405
406 endfunction
407
408 function retval = __plt2sv__ (h, x, y, options, properties)
409
410   if (nargin < 3 || nargin > 5)
411     print_usage ();
412   endif
413
414   if (nargin < 4 || isempty (options))
415     options = __default_plot_options__ ();
416   endif
417
418   if (nargin < 5)
419     properties = {};
420   endif
421
422   if (isscalar (x) && isvector (y))
423     len = numel (y);
424     if (numel (options) == 1)
425       options = repmat (options(:), len, 1);
426     endif
427     retval = zeros (len, 1);
428     for i = 1:len
429       linestyle = options(i).linestyle;
430       marker = options(i).marker;
431       if (isempty (marker) && isempty (linestyle))
432         [linestyle, marker] = __next_line_style__ ();
433       endif
434       color = options(i).color;
435       if (isempty (color))
436         color = __next_line_color__ ();
437       endif
438
439       retval(i) = line (x, y(i), "color", color,
440                         "linestyle", linestyle,
441                         "marker", marker, properties{:});
442     endfor
443   else
444     error ("__plt2sv__: first arg must be scalar, second arg must be vector");
445   endif
446
447 endfunction
448
449 function retval = __plt2vm__ (h, x, y, options, properties)
450
451   if (nargin < 3 || nargin > 5)
452     print_usage ();
453   endif
454
455   if (nargin < 4 || isempty (options))
456     options = __default_plot_options__ ();
457   endif
458
459   if (nargin < 5)
460     properties = {};
461   endif
462
463   [x_nr, x_nc] = size (x);
464   [y_nr, y_nc] = size (y);
465
466   if (x_nr == 1)
467     x = x';
468     tmp = x_nr;
469     x_nr = x_nc;
470     x_nc = tmp;
471   endif
472
473   if (x_nr == y_nr)
474     1;
475   elseif (x_nr == y_nc)
476     y = y';
477     tmp = y_nr;
478     y_nr = y_nc;
479     y_nc = tmp;
480   else
481     error ("__plt2vm__: matrix dimensions must match");
482   endif
483
484   if (y_nc > 0)
485     if (numel (options) == 1)
486       options = repmat (options(:), y_nc, 1);
487     endif
488     retval = zeros (y_nc, 1);
489     for i = 1:y_nc
490       linestyle = options(i).linestyle;
491       marker = options(i).marker;
492       if (isempty (marker) && isempty (linestyle))
493         [linestyle, marker] = __next_line_style__ ();
494       endif
495       color = options(i).color;
496       if (isempty (color))
497         color = __next_line_color__ ();
498       endif
499
500       retval(i) = line (x, y(:,i), "color", color,
501                         "linestyle", linestyle,
502                         "marker", marker, properties{:});
503     endfor
504   else
505     error ("__plt2vm__: arguments must be a matrices");
506   endif
507
508 endfunction
509
510 function retval = __plt2vs__ (h, x, y, options, properties)
511
512   if (nargin < 3 || nargin > 5)
513     print_usage ();
514   endif
515
516   if (nargin < 4 || isempty (options))
517     options = __default_plot_options__ ();
518   endif
519
520   if (nargin < 5)
521     properties = {};
522   endif
523
524   if (isvector (x) && isscalar (y))
525     len = numel (x);
526     if (numel (options) == 1)
527       options = repmat (options(:), len, 1);
528     endif
529     retval = zeros (len, 1);
530     for i = 1:len
531       linestyle = options(i).linestyle;
532       marker = options(i).marker;
533       if (isempty (marker) && isempty (linestyle))
534         [linestyle, marker] = __next_line_style__ ();
535       endif
536       color = options(i).color;
537       if (isempty (color))
538         color = __next_line_color__ ();
539       endif
540
541       retval(i) = line (x(i), y, "color", color,
542                         "linestyle", linestyle,
543                         "marker", marker, properties{:});
544     endfor
545   else
546     error ("__plt2vs__: first arg must be vector, second arg must be scalar");
547   endif
548
549 endfunction
550
551 function retval = __plt2vv__ (h, x, y, options, properties)
552
553   if (nargin < 3 || nargin > 5)
554     print_usage ();
555   endif
556
557   if (nargin < 4 || isempty (options))
558     options = __default_plot_options__ ();
559   endif
560
561   if (nargin < 5)
562     properties = {};
563   endif
564
565   if (numel (options) > 1)
566     options = options(1);
567   endif
568
569   [x_nr, x_nc] = size (x);
570   [y_nr, y_nc] = size (y);
571
572   if (x_nr == 1)
573     x = x';
574     tmp = x_nr;
575     x_nr = x_nc;
576     x_nc = tmp;
577   endif
578
579   if (y_nr == 1)
580     y = y';
581     tmp = y_nr;
582     y_nr = y_nc;
583     y_nc = tmp;
584   endif
585
586   if (x_nr == y_nr)
587     linestyle = options.linestyle;
588     marker = options.marker;
589     if (isempty (marker) && isempty (linestyle))
590       [linestyle, marker] = __next_line_style__ ();
591     endif
592     color = options.color;
593     if (isempty (color))
594       color = __next_line_color__ ();
595     endif
596
597     retval = line (x, y, "color", color,
598               "linestyle", linestyle,
599               "marker", marker, properties{:});
600   else
601     error ("__plt2vv__: vector lengths must match");
602   endif
603
604 endfunction