File: rticklabels.m

package info (click to toggle)
octave 10.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 145,388 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 660; xml: 192
file content (260 lines) | stat: -rwxr-xr-x 9,383 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
########################################################################
##
## Copyright (C) 2017-2025 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave 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.
##
## Octave 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 Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn  {} {@var{labels} =} rticklabels
## @deftypefnx {} {} rticklabels (@var{tickval})
## @deftypefnx {} {@dots{} =} rticklabels (@var{hax}, @dots{})
## Query or set the tick labels on the r-axis of a polar plot.
##
## When called without an argument, return a cell array of strings of the
## current rtick labels.
##
## When called with the argument @var{tickval} being a vector of numbers or
## a cell array of strings and/or numbers, the labels will be changed to
## match these new values.  Note that the center point of the plots made by
## @code{polar} are never labeled, so the first specified label will be
## applied to the second rtick location and subesquent labels will progress
## outward.
##
## If fewer labels are specified than the current number of tick marks, those
## labels will be applied starting with the innermost tick labels, and blank
## labels will be appended to the remainder.  If the specified label count
## exceeds the number of tick labels, the excess labels are ignored.
##
## If the first argument @var{hax} is an axes handle, then operate on
## this axis rather than the current axes returned by @code{gca}.
##
## Requesting a return value when calling @code{rticklabels} to set a property
## value will result in an error.
##
## Compatability Note: The @qcode{'mode'} property for rticklabels has not yet
## been implemented.
##
## @seealso{polar, rticks, thetaticklabels, xticklabels, yticklabels,
## zticklabels, get, set}
## @end deftypefn

## FIXME:  Octave's polar plot implementation does not currently create the
##         properties rticklabel, rticklabelmode, and rtickmode.  Fully
##         implemented versions of those proporties could simplify much of the
##         code below, which could then mimick much the behavior of the
##         equivalent Cartesian functions.

function labels = rticklabels (varargin)

  [hax, varargin, nargs] = __plt_get_axis_arg__ ("rticklabels", varargin{:});

  if (nargs > 1)
    print_usage;
  endif

  returnlabels = true;

  ## Check first input for axes handle and remove from argument list.
  if (nargs > 0)
    ## Single remaining input must be tick labels and should be a numeric
    ## vector or a cell vector of numbers and strings.

    ## Error if trying to request and set values simultaneously.
    if (nargout > 0)
      error ("rticklabels: cannot set and return labels simultaneously");
    endif

    returnlabels = false;
    arg = varargin{1};

    if (isnumeric (arg))
      ## All inputs handled the same way as cells. (:) permits nonvectors.
      cellarg_num = ones (1, numel (arg));
      cellarg_char = zeros (1, numel (arg));
      arg = num2cell (arg(:));

    elseif (iscell (arg))

      if (! all ((cellarg_num = cellfun ('isnumeric', arg))
                           | (cellarg_char = cellfun ('ischar', arg))))
        error ("rticklabels: TICKVAL cell must contain numbers or strings");
      endif

    else
      error (["rticklabels: TICKVAL must be numeric or a cell ", ...
              "containing numbers and strings"]);
    endif

  ## Finish converting TICVAL into a cellstr.

  ## Convert numeric elements to characters and make it a 1-D cell array.
  arg(cellarg_num) = cellfun (@num2str, arg(cellarg_num), ...
                                "UniformOutput", false);
  arg = arg(:);
  endif

  if (isempty (hax))
    hax = gca ();
  endif

  ## Error if the remaining input
  polarhandle = findall (hax, "tag", "polar_grid");

  ## Error if hax does not point to a polar plot with r elements.
  if (isempty (polarhandle))
    error ("rticklabels: rticklabels can only be used on a polar plot");
  elseif (! isfield (get (hax), "rtick") )
    error ("rticklabels: rtick property not defined for current axes");
  endif

  ## Get count of radius and theta curves.
  nr = numel (get (hax, "rtick")); # Must check if it includes unlabeled center.
  nt = numel (get (hax, "ttick"));
  rt_label_handles = get (polarhandle, "children");

  ## Center is never labeled in polar plots.
  ## If rtick does not include unlabled center, label_count = 2*nr+2*nt-1,
  ## otherwise label_count = 2*nr+2*nt+1 and nr should be reduced by 1
  ## to prevent attempts to get label value from nonlabel handle.
  if (2 * (nr + nt) + 1 > numel (rt_label_handles))
    nr--;
  endif

  ## rtick and ttick object ordering:
  ##  1:nt = text handles containing theta labels (reverse order)
  ##  nt+1:2*nt = line object handles for ttick radial grid lines
  ##  2*nt+1:2*nt+nr = text handles containing nr rtick labels (reverse order)
  ##  2*nt+nr+1:2*nt+2*nr = line object handles for rtick circles
  ##  end = patch object handle for darker outside border

  rlabelrange = 2*nt + [nr:-1:1]; # Flip back to increasing order.

  if (nargs == 0)
    ## Get radius labels.
    labels = get (rt_label_handles(rlabelrange), "string");

  else
    ## Set new label values.

    if (isempty (arg))
      ## If an empty cell or array is set, set all labels to "".
      arg = cell (nr, 1);
      arg(:) = {""};

    else
      ## Pad and trim arg as needed to match number of labels.
      if (nr < numel (arg))
        arg (nr + 1 : end) = [];
      else
        arg(end + 1 : nr) = {""};
      endif
    endif

    ## Replace labels with values from arg, ensure column vector.
    set (rt_label_handles(rlabelrange), {"string"}, arg(:));
  endif

endfunction


%!test
%! t = [0:15:180] * pi / 180;
%! r = sin (t);
%! hf = figure ("visible", "off");
%! unwind_protect
%!   hp = polar (t, r);
%!   hax = gca ();
%!   rticks (hax, [0, 0.5, 1]);
%!   assert (rticklabels (hax), {"0.5"; "1"});
%!   rticklabels (hax, [1 2]);
%!   assert (rticklabels (hax), {"1"; "2"});
%!   rticklabels (hax, [3 4]');
%!   assert (rticklabels (hax), {"3"; "4"});
%!   rticklabels (hax, {5, 6});
%!   assert (rticklabels (hax), {"5"; "6"});
%!   rticklabels (hax, {"a", "bee"});
%!   assert (rticklabels (hax), {"a"; "bee"});
%!   rticklabels (hax, {1, "two"});
%!   assert (rticklabels (hax), {"1"; "two"});
%!   rticklabels (hax, {1, 2, 3, 4});
%!   assert (rticklabels (hax), {"1"; "2"});
%!   rticklabels (hax, [5, 6, 7, 8]);
%!   assert (rticklabels (hax), {"5"; "6"});
%!   rticklabels (hax, {1});
%!   assert (rticklabels (hax), {"1"; ""});
%!   rticklabels (hax, {1,2});
%!   assert (rticklabels (hax), {"1"; "2"});
%!   rticklabels (hax, []);
%!   assert (rticklabels (hax), {""; ""});
%!   rticklabels (hax, {1,2});
%!   assert (rticklabels (hax), {"1"; "2"});
%!   rticklabels (hax, {});
%!   assert (rticklabels (hax), {""; ""});
%! unwind_protect_cleanup
%!   close (hf);
%! end_unwind_protect


## Test input validation
%!test
%! hf = figure ("visible", "off");
%! unwind_protect
%!   hax = gca ();
%!   fail ("rticklabels (1,2,3)", "Invalid call");
%!   fail ("rticklabels (-1, 2)", "Invalid call");
%!   fail ("rticklabels (hax, 2, 3)", "Invalid call");
%!   fail ("rticklabels (hf, 2)", "Invalid call");
%! unwind_protect_cleanup
%!   close (hf);
%! end_unwind_protect

%!test
%! hf = figure ("visible", "off");
%! unwind_protect
%!   hax = gca ();
%!   fail ("tmp = rticklabels ([1, 2, 3])", "cannot set and return labels simultaneously");
%!   fail ("tmp = rticklabels (hax, [1, 2, 3])", "cannot set and return labels simultaneously");
%!   fail ("rticklabels (hax, {{1}, 'two', 3})", "TICKVAL cell must contain numbers or strings");
%!   fail ("rticklabels (hax, {1, 'two', false})", "TICKVAL cell must contain numbers or strings");
%!   fail ("rticklabels (hax, {1, struct()})", "TICKVAL cell must contain numbers or strings");
%!   fail ("rticklabels (hax, struct())", "TICKVAL must be numeric or a cell containing");
%!   fail ("rticklabels (hax, [true false])", "TICKVAL must be numeric or a cell containing");
%! unwind_protect_cleanup
%!   close (hf);
%! end_unwind_protect

%!test
%! t = [0:45:180] * pi / 180;
%! r = sin (t);
%! hf = figure ("visible", "off");
%! unwind_protect
%!   hp = plot (t, r);
%!   hax = gca ();
%!   fail ("rticklabels (hax, 1)", "rticklabels can only be used on a polar plot");
%!   fail ("rticklabels ({1})", "rticklabels can only be used on a polar plot");
%!   hp = polar (t, r);
%!   hax = gca ();
%!   delete (findall (hax, "tag", "polar_grid"));
%!   fail ("rticklabels (hax, 1)", "rticklabels can only be used on a polar plot");
%! unwind_protect_cleanup
%!   close (hf);
%! end_unwind_protect