Mercurial > hg > octave-nkf
annotate scripts/testfun/demo.m @ 9051:1bf0ce0930be
Grammar check TexInfo in all .m files
Cleanup documentation sources to follow a few consistent rules.
Spellcheck was NOT done. (but will be in another changeset)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 27 Mar 2009 22:31:03 -0700 |
parents | eb63fbe60fab |
children | 693e22af08ae |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2006, 2007, 2008, 2009 Paul Kienzle |
5589 | 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} {} demo ('@var{name}',@var{n}) | |
21 ## | |
22 ## Runs any examples associated with the function '@var{name}'. | |
23 ## Examples are stored in the script file, or in a file with the same | |
24 ## name but no extension somewhere on your path. To keep them separate | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## from the usual script code, all lines are prefixed by @code{%!}. Each |
5589 | 26 ## example is introduced by the keyword 'demo' flush left to the prefix, |
27 ## with no intervening spaces. The remainder of the example can contain | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
28 ## arbitrary Octave code. For example: |
5589 | 29 ## |
30 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
31 ## @group |
5589 | 32 ## %!demo |
33 ## %! t=0:0.01:2*pi; x = sin(t); | |
34 ## %! plot(t,x) | |
35 ## %! %------------------------------------------------- | |
36 ## %! % the figure window shows one cycle of a sine wave | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
37 ## @end group |
5589 | 38 ## @end example |
39 ## | |
40 ## Note that the code is displayed before it is executed, so a simple | |
41 ## comment at the end suffices. It is generally not necessary to use | |
42 ## disp or printf within the demo. | |
43 ## | |
44 ## Demos are run in a function environment with no access to external | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
45 ## variables. This means that all demos in your function must use |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
46 ## separate initialization code. Alternatively, you can combine your |
5589 | 47 ## demos into one huge demo, with the code: |
48 ## | |
49 ## @example | |
50 ## %! input("Press <enter> to continue: ","s"); | |
51 ## @end example | |
52 ## | |
53 ## between the sections, but this is discouraged. Other techniques | |
54 ## include using multiple plots by saying figure between each, or | |
55 ## using subplot to put multiple plots in the same window. | |
56 ## | |
57 ## Also, since demo evaluates inside a function context, you cannot | |
58 ## define new functions inside a demo. Instead you will have to | |
59 ## use @code{eval(example('function',n))} to see them. Because eval only | |
60 ## evaluates one line, or one statement if the statement crosses | |
61 ## multiple lines, you must wrap your demo in "if 1 <demo stuff> endif" | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
62 ## with the 'if' on the same line as 'demo'. For example, |
5589 | 63 ## |
64 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
65 ## @group |
5589 | 66 ## %!demo if 1 |
67 ## %! function y=f(x) | |
68 ## %! y=x; | |
69 ## %! endfunction | |
70 ## %! f(3) | |
71 ## %! endif | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
72 ## @end group |
5589 | 73 ## @end example |
5642 | 74 ## @seealso{test, example} |
5589 | 75 ## @end deftypefn |
76 | |
8202
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7540
diff
changeset
|
77 ## FIXME: modify subplot so that gnuplot_has_multiplot == 0 causes it to |
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7540
diff
changeset
|
78 ## use the current figure window but pause if not plotting in the |
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7540
diff
changeset
|
79 ## first subplot. |
5589 | 80 |
6494 | 81 function demo (name, n) |
5589 | 82 |
83 if (nargin < 1 || nargin > 2) | |
6046 | 84 print_usage (); |
5589 | 85 endif |
86 | |
87 if (nargin < 2) | |
88 n = 0; | |
89 endif | |
90 | |
6494 | 91 [code, idx] = test (name, "grabdemo"); |
92 if (length (idx) == 0) | |
93 warning ("demo not available for %s", name); | |
5589 | 94 return; |
6494 | 95 elseif (n >= length (idx)) |
96 warning ("only %d demos available for %s", length (idx) - 1, name); | |
5589 | 97 return; |
98 endif | |
99 | |
100 | |
101 if (n > 0) | |
102 doidx = n; | |
103 else | |
6494 | 104 doidx = 1:length(idx)-1; |
5589 | 105 endif |
6494 | 106 for i = 1:length (doidx) |
5589 | 107 ## Pause between demos |
108 if (i > 1) | |
6494 | 109 input ("Press <enter> to continue: ", "s"); |
5589 | 110 endif |
111 | |
112 ## Process each demo without failing | |
113 try | |
6494 | 114 block = code(idx(doidx(i)):idx(doidx(i)+1)-1); |
5589 | 115 ## Use an environment without variables |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
116 eval (cstrcat ("function __demo__()\n", block, "\nendfunction")); |
5589 | 117 ## Display the code that will be executed before executing it |
6494 | 118 printf ("%s example %d:%s\n\n", name, doidx(i), block); |
5589 | 119 __demo__; |
120 catch | |
121 ## Let the programmer know which demo failed. | |
6494 | 122 printf ("%s example %d: failed\n%s", name, doidx(i), __error_text__); |
5589 | 123 end_try_catch |
124 clear __demo__; | |
125 endfor | |
126 | |
127 endfunction | |
128 | |
129 %!demo | |
130 %! t=0:0.01:2*pi; x = sin(t); | |
131 %! plot(t,x) | |
132 %! %------------------------------------------------- | |
133 %! % the figure window shows one cycle of a sine wave |