1 ## Copyright (C) 2000-2012 Paul Kienzle
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Command} {} example @var{name}
21 ## @deftypefnx {Command} {} example @var{name} @var{n}
22 ## @deftypefnx {Function File} {} example ('@var{name}')
23 ## @deftypefnx {Function File} {} example ('@var{name}', @var{n})
24 ## @deftypefnx {Function File} {[@var{s}, @var{idx}] =} example (@dots{})
26 ## Display the code for example @var{n} associated with the function
27 ## '@var{name}', but do not run it. If @var{n} is not specified, all examples
30 ## When called with output arguments, the examples are returned in the form of
31 ## a string @var{s}, with @var{idx} indicating the ending position of the
34 ## See @code{demo} for a complete explanation.
35 ## @seealso{demo, test}
38 function [code_r, idx_r] = example (name, n)
40 if (nargin < 1 || nargin > 2)
50 [code, idx] = test (name, "grabdemo");
53 if (n <= length (idx))
54 code_r = code(idx(n):idx(n+1)-1);
55 idx_r = [1, length(code_r)+1];
68 doidx = 1:length(idx)-1;
71 warning ("no example available for %s", name);
73 elseif (n >= length(idx))
74 warning ("only %d examples available for %s", length(idx)-1, name);
78 for i = 1:length (doidx)
79 block = code(idx(doidx(i)):idx(doidx(i)+1)-1);
80 printf ("%s example %d:%s\n\n", name, doidx(i), block);
87 %!## warning: don't modify the demos without modifying the tests!
89 %! example ('example');
91 %! t=0:0.01:2*pi; x = sin(t);
94 %!assert (example('example',1), "\n example ('example');");
96 %! [code, idx] = example ('example');
98 %! "\n example ('example');\n t=0:0.01:2*pi; x = sin(t);\n plot (t,x)")
99 %! assert (idx, [1, 23, 63]);
101 %% Test input validation
103 %!error example ('example', 3, 5)