5589
|
1 ## Copyright (C) 2000 Paul Kienzle |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
16 ## 02110-1301 USA |
|
17 |
|
18 ## -*- texinfo -*- |
|
19 ## @deftypefn {Function File} {} example ('@var{name}',@var{n}) |
|
20 ## @deftypefnx {Function File} {[@var{x}, @var{idx}] =} example ('@var{name}',@var{n}) |
|
21 ## |
|
22 ## Display the code for example @var{n} associated with the function |
|
23 ## '@var{name}', but do not run it. If @var{n} is not given, all examples |
|
24 ## are displayed. |
|
25 ## |
|
26 ## Called with output arguments, the examples are returned in the form of |
|
27 ## a string @var{x}, with @var{idx} indicating the ending position of the |
|
28 ## various examples. |
|
29 ## |
|
30 ## See @code{demo} for a complete explanation. |
5642
|
31 ## @seealso{demo, test} |
5589
|
32 ## @end deftypefn |
|
33 |
|
34 ## PKG_ADD: mark_as_command example |
|
35 |
6494
|
36 function [code_r, idx_r] = example (name, n) |
5589
|
37 |
|
38 if (nargin < 1 || nargin > 2) |
6046
|
39 print_usage (); |
5589
|
40 endif |
|
41 if (nargin < 2) |
|
42 n = 0; |
|
43 endif |
|
44 |
6494
|
45 [code, idx] = test (name, "grabdemo"); |
5589
|
46 if (nargout > 0) |
|
47 if (n > 0) |
6494
|
48 if (n <= length (idx)) |
|
49 code_r = code(idx(n):idx(n+1)-1); |
5589
|
50 idx_r = [1, length(code_r)+1]; |
|
51 else |
|
52 code_r = ""; |
|
53 idx_r = []; |
|
54 endif |
|
55 else |
|
56 code_r = code; |
|
57 idx_r = idx; |
|
58 endif |
|
59 else |
|
60 if (n > 0) |
|
61 doidx = n; |
|
62 else |
6494
|
63 doidx = 1:length(idx)-1; |
5589
|
64 endif |
6494
|
65 if (length (idx) == 0) |
|
66 warning ("example not available for %s", name); |
5589
|
67 elseif (n >= length(idx)) |
6494
|
68 warning ("only %d examples available for %s", length(idx)-1, name); |
5589
|
69 doidx = []; |
|
70 endif |
|
71 |
|
72 for i=1:length(doidx) |
6494
|
73 block = code(idx(doidx(i)):idx(doidx(i)+1)-1); |
|
74 printf ("%s example %d:%s\n\n", name, doidx(i), block); |
5589
|
75 endfor |
|
76 endif |
|
77 |
|
78 endfunction |
|
79 |
|
80 %!## warning: don't modify the demos without modifying the tests! |
|
81 %!demo |
|
82 %! example('example'); |
|
83 %!demo |
|
84 %! t=0:0.01:2*pi; x=sin(t); |
|
85 %! plot(t,x) |
|
86 |
|
87 %!assert (example('example',1), "\n example('example');"); |
|
88 %!test |
|
89 %! [code, idx] = example('example'); |
|
90 %! assert (code, ... |
|
91 %! "\n example('example');\n t=0:0.01:2*pi; x=sin(t);\n plot(t,x)") |
|
92 %! assert (idx, [1, 22, 59]); |
|
93 |
|
94 %!error example; |
|
95 %!error example('example',3,5) |