Mercurial > hg > octave-lyh
comparison scripts/testfun/demo.m @ 5589:f812a0680d05
[project @ 2006-01-06 00:14:42 by jwe]
author | jwe |
---|---|
date | Fri, 06 Jan 2006 00:14:42 +0000 |
parents | |
children | 2618a0750ae6 |
comparison
equal
deleted
inserted
replaced
5588:79ec73a1ff15 | 5589:f812a0680d05 |
---|---|
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} {} demo ('@var{name}',@var{n}) | |
20 ## | |
21 ## Runs any examples associated with the function '@var{name}'. | |
22 ## Examples are stored in the script file, or in a file with the same | |
23 ## name but no extension somewhere on your path. To keep them separate | |
24 ## from the usual script code, all lines are prefixed by @code{%!}. Each | |
25 ## example is introduced by the keyword 'demo' flush left to the prefix, | |
26 ## with no intervening spaces. The remainder of the example can contain | |
27 ## arbitrary octave code. For example: | |
28 ## | |
29 ## @example | |
30 ## %!demo | |
31 ## %! t=0:0.01:2*pi; x = sin(t); | |
32 ## %! plot(t,x) | |
33 ## %! %------------------------------------------------- | |
34 ## %! % the figure window shows one cycle of a sine wave | |
35 ## @end example | |
36 ## | |
37 ## Note that the code is displayed before it is executed, so a simple | |
38 ## comment at the end suffices. It is generally not necessary to use | |
39 ## disp or printf within the demo. | |
40 ## | |
41 ## Demos are run in a function environment with no access to external | |
42 ## variables. This means that all demos in your function must use | |
43 ## separate initialization code. Alternatively, you can combine your | |
44 ## demos into one huge demo, with the code: | |
45 ## | |
46 ## @example | |
47 ## %! input("Press <enter> to continue: ","s"); | |
48 ## @end example | |
49 ## | |
50 ## between the sections, but this is discouraged. Other techniques | |
51 ## include using multiple plots by saying figure between each, or | |
52 ## using subplot to put multiple plots in the same window. | |
53 ## | |
54 ## Also, since demo evaluates inside a function context, you cannot | |
55 ## define new functions inside a demo. Instead you will have to | |
56 ## use @code{eval(example('function',n))} to see them. Because eval only | |
57 ## evaluates one line, or one statement if the statement crosses | |
58 ## multiple lines, you must wrap your demo in "if 1 <demo stuff> endif" | |
59 ## with the 'if' on the same line as 'demo'. For example, | |
60 ## | |
61 ## @example | |
62 ## %!demo if 1 | |
63 ## %! function y=f(x) | |
64 ## %! y=x; | |
65 ## %! endfunction | |
66 ## %! f(3) | |
67 ## %! endif | |
68 ## @end example | |
69 ## | |
70 ## @end deftypefn | |
71 ## @seealso{test, example} | |
72 | |
73 ## TODO: modify subplot so that gnuplot_has_multiplot == 0 causes it to | |
74 ## TODO: use the current figure window but pause if not plotting in the | |
75 ## TODO: first subplot. | |
76 | |
77 ## PKG_ADD: mark_as_command demo | |
78 | |
79 function demo(name, n) | |
80 | |
81 if (nargin < 1 || nargin > 2) | |
82 usage("demo('name') or demo('name, n)"); | |
83 endif | |
84 | |
85 if (nargin < 2) | |
86 n = 0; | |
87 endif | |
88 | |
89 [code, idx] = test (name, 'grabdemo'); | |
90 if (length(idx) == 0) | |
91 warning(["demo not available for ", name]); | |
92 return; | |
93 elseif (n >= length(idx)) | |
94 warning(sprintf("only %d demos available for %s", length(idx)-1, name)); | |
95 return; | |
96 endif | |
97 | |
98 | |
99 if (n > 0) | |
100 doidx = n; | |
101 else | |
102 doidx = [ 1 : length(idx)-1 ]; | |
103 endif | |
104 for i=1:length(doidx) | |
105 ## Pause between demos | |
106 if (i > 1) | |
107 input("Press <enter> to continue: ","s"); | |
108 endif | |
109 | |
110 ## Process each demo without failing | |
111 try | |
112 block = code( idx(doidx(i)) : idx(doidx(i)+1) -1 ); | |
113 ## Use an environment without variables | |
114 eval(["function __demo__()\n", block, "\nendfunction"]); | |
115 ## Display the code that will be executed before executing it | |
116 printf("%s example %d:%s\n\n", name, doidx(i), block); | |
117 __demo__; | |
118 catch | |
119 ## Let the programmer know which demo failed. | |
120 printf("%s example %d: failed\n%s", name, doidx(i), __error_text__); | |
121 end_try_catch | |
122 clear __demo__; | |
123 endfor | |
124 | |
125 endfunction | |
126 | |
127 %!demo | |
128 %! t=0:0.01:2*pi; x = sin(t); | |
129 %! plot(t,x) | |
130 %! %------------------------------------------------- | |
131 %! % the figure window shows one cycle of a sine wave |