Mercurial > hg > octave-nkf
annotate scripts/testfun/demo.m @ 12329:5f203b5bbf98
Use testif to only run some sparse tests when necessary libraries are installed.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 02 Feb 2011 21:31:33 -0800 |
parents | c792872f8942 |
children | 6a1fe83fe129 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 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 -*- | |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
20 ## @deftypefn {Function File} {} demo ('@var{name}', @var{n}) |
5589 | 21 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
22 ## Runs any examples associated with the function '@var{name}'. |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
23 ## Examples are stored in the script file, or in a file with the same |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
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, |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
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 ## | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
53 ## @noindent |
5589 | 54 ## between the sections, but this is discouraged. Other techniques |
55 ## include using multiple plots by saying figure between each, or | |
56 ## using subplot to put multiple plots in the same window. | |
57 ## | |
58 ## Also, since demo evaluates inside a function context, you cannot | |
59 ## define new functions inside a demo. Instead you will have to | |
60 ## use @code{eval(example('function',n))} to see them. Because eval only | |
61 ## evaluates one line, or one statement if the statement crosses | |
62 ## multiple lines, you must wrap your demo in "if 1 <demo stuff> endif" | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
63 ## with the 'if' on the same line as 'demo'. For example: |
5589 | 64 ## |
65 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
66 ## @group |
5589 | 67 ## %!demo if 1 |
68 ## %! function y=f(x) | |
69 ## %! y=x; | |
70 ## %! endfunction | |
71 ## %! f(3) | |
72 ## %! endif | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
73 ## @end group |
5589 | 74 ## @end example |
5642 | 75 ## @seealso{test, example} |
5589 | 76 ## @end deftypefn |
77 | |
8202
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7540
diff
changeset
|
78 ## 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
|
79 ## 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
|
80 ## first subplot. |
5589 | 81 |
6494 | 82 function demo (name, n) |
5589 | 83 |
84 if (nargin < 1 || nargin > 2) | |
6046 | 85 print_usage (); |
5589 | 86 endif |
87 | |
88 if (nargin < 2) | |
89 n = 0; | |
90 endif | |
91 | |
6494 | 92 [code, idx] = test (name, "grabdemo"); |
93 if (length (idx) == 0) | |
94 warning ("demo not available for %s", name); | |
5589 | 95 return; |
6494 | 96 elseif (n >= length (idx)) |
97 warning ("only %d demos available for %s", length (idx) - 1, name); | |
5589 | 98 return; |
99 endif | |
100 | |
101 | |
102 if (n > 0) | |
103 doidx = n; | |
104 else | |
6494 | 105 doidx = 1:length(idx)-1; |
5589 | 106 endif |
6494 | 107 for i = 1:length (doidx) |
5589 | 108 ## Pause between demos |
109 if (i > 1) | |
6494 | 110 input ("Press <enter> to continue: ", "s"); |
5589 | 111 endif |
112 | |
113 ## Process each demo without failing | |
114 try | |
6494 | 115 block = code(idx(doidx(i)):idx(doidx(i)+1)-1); |
5589 | 116 ## 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
|
117 eval (cstrcat ("function __demo__()\n", block, "\nendfunction")); |
5589 | 118 ## Display the code that will be executed before executing it |
6494 | 119 printf ("%s example %d:%s\n\n", name, doidx(i), block); |
5589 | 120 __demo__; |
121 catch | |
122 ## Let the programmer know which demo failed. | |
11437
6bfb286a0efa
Add newline to demo error reporting for better formatting.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
123 printf ("%s example %d: failed\n%s\n", name, doidx(i), __error_text__); |
5589 | 124 end_try_catch |
125 clear __demo__; | |
126 endfor | |
127 | |
128 endfunction | |
129 | |
130 %!demo | |
131 %! t=0:0.01:2*pi; x = sin(t); | |
132 %! plot(t,x) | |
133 %! %------------------------------------------------- | |
134 %! % the figure window shows one cycle of a sine wave |