5589
|
1 ## Copyright (C) 2000 Paul Kienzle |
|
2 ## |
7016
|
3 ## This file is part of Octave. |
5589
|
4 ## |
7016
|
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. |
5589
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5589
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} example ('@var{name}',@var{n}) |
|
21 ## @deftypefnx {Function File} {[@var{x}, @var{idx}] =} example ('@var{name}',@var{n}) |
|
22 ## |
|
23 ## Display the code for example @var{n} associated with the function |
|
24 ## '@var{name}', but do not run it. If @var{n} is not given, all examples |
|
25 ## are displayed. |
|
26 ## |
|
27 ## Called with output arguments, the examples are returned in the form of |
|
28 ## a string @var{x}, with @var{idx} indicating the ending position of the |
|
29 ## various examples. |
|
30 ## |
|
31 ## See @code{demo} for a complete explanation. |
5642
|
32 ## @seealso{demo, test} |
5589
|
33 ## @end deftypefn |
|
34 |
|
35 ## PKG_ADD: mark_as_command example |
|
36 |
6494
|
37 function [code_r, idx_r] = example (name, n) |
5589
|
38 |
|
39 if (nargin < 1 || nargin > 2) |
6046
|
40 print_usage (); |
5589
|
41 endif |
|
42 if (nargin < 2) |
|
43 n = 0; |
|
44 endif |
|
45 |
6494
|
46 [code, idx] = test (name, "grabdemo"); |
5589
|
47 if (nargout > 0) |
|
48 if (n > 0) |
6494
|
49 if (n <= length (idx)) |
|
50 code_r = code(idx(n):idx(n+1)-1); |
5589
|
51 idx_r = [1, length(code_r)+1]; |
|
52 else |
|
53 code_r = ""; |
|
54 idx_r = []; |
|
55 endif |
|
56 else |
|
57 code_r = code; |
|
58 idx_r = idx; |
|
59 endif |
|
60 else |
|
61 if (n > 0) |
|
62 doidx = n; |
|
63 else |
6494
|
64 doidx = 1:length(idx)-1; |
5589
|
65 endif |
6494
|
66 if (length (idx) == 0) |
|
67 warning ("example not available for %s", name); |
5589
|
68 elseif (n >= length(idx)) |
6494
|
69 warning ("only %d examples available for %s", length(idx)-1, name); |
5589
|
70 doidx = []; |
|
71 endif |
|
72 |
|
73 for i=1:length(doidx) |
6494
|
74 block = code(idx(doidx(i)):idx(doidx(i)+1)-1); |
|
75 printf ("%s example %d:%s\n\n", name, doidx(i), block); |
5589
|
76 endfor |
|
77 endif |
|
78 |
|
79 endfunction |
|
80 |
|
81 %!## warning: don't modify the demos without modifying the tests! |
|
82 %!demo |
|
83 %! example('example'); |
|
84 %!demo |
|
85 %! t=0:0.01:2*pi; x=sin(t); |
|
86 %! plot(t,x) |
|
87 |
|
88 %!assert (example('example',1), "\n example('example');"); |
|
89 %!test |
|
90 %! [code, idx] = example('example'); |
|
91 %! assert (code, ... |
|
92 %! "\n example('example');\n t=0:0.01:2*pi; x=sin(t);\n plot(t,x)") |
|
93 %! assert (idx, [1, 22, 59]); |
|
94 |
|
95 %!error example; |
|
96 %!error example('example',3,5) |