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
|
########################################################################
##
## Copyright (C) 2000-2024 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 {} {} example @var{name}
## @deftypefnx {} {} example @var{name} @var{n}
## @deftypefnx {} {} example ("@var{name}")
## @deftypefnx {} {} example ("@var{name}", @var{n})
## @deftypefnx {} {[@var{codestr}, @var{codeidx}] =} example (@dots{})
##
## Display the code for example @var{n} associated with the function
## @var{name}, but do not run it.
##
## If @var{n} is not specified, all examples are displayed.
##
## When called with output arguments, the examples are returned in the form of
## a string @var{codestr}, with @var{codeidx} indicating the ending position of
## the various examples.
##
## For a complete explanation @pxref{XREFdemo,,@code{demo}}.
## @seealso{demo, test}
## @end deftypefn
function [codestr, codeidx] = example (name, n = 0)
if (nargin < 1)
print_usage ();
endif
if (ischar (n))
n = str2double (n);
endif
if (! (isreal (n) && isscalar (n) && n == fix (n)))
error ("example: N must be a scalar integer");
endif
[code, idx] = test (name, "grabdemo");
if (idx == -1)
warning ("example: no function %s found", name);
return;
elseif (isempty (idx))
warning ("example: no example available for %s", name);
return;
elseif (n >= length (idx))
warning ("example: only %d examples available for %s",
length (idx) - 1, name);
return;
endif
if (nargout > 0)
if (n > 0)
if (n <= length (idx))
codestr = code(idx(n):idx(n+1)-1);
codeidx = [1, length(codestr)+1];
else
codestr = "";
codeidx = [];
endif
else
codestr = code;
codeidx = idx;
endif
else
if (n > 0)
doidx = n;
else
doidx = 1:length (idx) - 1;
endif
for i = 1:length (doidx)
block = code(idx(doidx(i)):idx(doidx(i)+1)-1);
printf ("%s example %d:%s\n\n", name, doidx(i), block);
endfor
endif
endfunction
## WARNING: don't modify the demos without modifying the tests!
%!demo
%! example ("example");
%!demo
%! clf;
%! t = 0:0.01:2*pi;
%! x = sin (t);
%! plot (t,x)
%!assert (example ("example", 1), "\n example (\"example\");")
%!test
%! [code, idx] = example ("example");
%! assert (code, ...
%! "\n example (\"example\");\n clf;\n t = 0:0.01:2*pi;\n x = sin (t);\n plot (t,x)");
%! assert (idx, [1, 23, 73]);
## Test input validation
%!error <Invalid call> example ()
%!error <N must be a scalar integer> example ("example", {1})
%!error <N must be a scalar integer> example ("example", ones (2,2))
%!error <N must be a scalar integer> example ("example", 1.5)
%!warning <no function .* found> example ("_%NOT_A_FUNCTION%_");
%!warning <only 2 examples available for example> example ("example", 10);
|