Mercurial > hg > octave-nkf
annotate scripts/testfun/demo.m @ 12556:88558b8eb8a7
Add deprecated entry for cquad() pointing to quadcc().
HG Enter commit message. Lines beginning with 'HG:' are removed.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Thu, 31 Mar 2011 09:57:11 -0700 |
parents | 6a1fe83fe129 |
children | 55430618bd5f |
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 -*- | |
12505
6a1fe83fe129
Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
20 ## @deftypefn {Command} {} demo @var{name} @var{n} |
6a1fe83fe129
Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
21 ## @deftypefnx {Function File} {} demo ('@var{name}', @var{n}) |
5589 | 22 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
23 ## 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
|
24 ## 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
|
25 ## 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
|
26 ## from the usual script code, all lines are prefixed by @code{%!}. Each |
5589 | 27 ## 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
|
28 ## 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
|
29 ## arbitrary Octave code. For example: |
5589 | 30 ## |
31 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
32 ## @group |
5589 | 33 ## %!demo |
34 ## %! t=0:0.01:2*pi; x = sin(t); | |
35 ## %! plot(t,x) | |
36 ## %! %------------------------------------------------- | |
37 ## %! % 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
|
38 ## @end group |
5589 | 39 ## @end example |
40 ## | |
41 ## Note that the code is displayed before it is executed, so a simple | |
42 ## comment at the end suffices. It is generally not necessary to use | |
43 ## disp or printf within the demo. | |
44 ## | |
45 ## 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
|
46 ## 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
|
47 ## separate initialization code. Alternatively, you can combine your |
5589 | 48 ## demos into one huge demo, with the code: |
49 ## | |
50 ## @example | |
51 ## %! input("Press <enter> to continue: ","s"); | |
52 ## @end example | |
53 ## | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
54 ## @noindent |
5589 | 55 ## between the sections, but this is discouraged. Other techniques |
56 ## include using multiple plots by saying figure between each, or | |
57 ## using subplot to put multiple plots in the same window. | |
58 ## | |
59 ## Also, since demo evaluates inside a function context, you cannot | |
60 ## define new functions inside a demo. Instead you will have to | |
61 ## use @code{eval(example('function',n))} to see them. Because eval only | |
62 ## evaluates one line, or one statement if the statement crosses | |
63 ## 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
|
64 ## with the 'if' on the same line as 'demo'. For example: |
5589 | 65 ## |
66 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
67 ## @group |
5589 | 68 ## %!demo if 1 |
69 ## %! function y=f(x) | |
70 ## %! y=x; | |
71 ## %! endfunction | |
72 ## %! f(3) | |
73 ## %! endif | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
74 ## @end group |
5589 | 75 ## @end example |
5642 | 76 ## @seealso{test, example} |
5589 | 77 ## @end deftypefn |
78 | |
8202
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7540
diff
changeset
|
79 ## 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
|
80 ## 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
|
81 ## first subplot. |
5589 | 82 |
6494 | 83 function demo (name, n) |
5589 | 84 |
85 if (nargin < 1 || nargin > 2) | |
6046 | 86 print_usage (); |
5589 | 87 endif |
88 | |
89 if (nargin < 2) | |
90 n = 0; | |
12505
6a1fe83fe129
Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
91 elseif (strcmp ("char", class (n))) |
6a1fe83fe129
Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
92 n = str2double (n); |
6a1fe83fe129
Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
93 endif |
5589 | 94 |
6494 | 95 [code, idx] = test (name, "grabdemo"); |
96 if (length (idx) == 0) | |
97 warning ("demo not available for %s", name); | |
5589 | 98 return; |
6494 | 99 elseif (n >= length (idx)) |
100 warning ("only %d demos available for %s", length (idx) - 1, name); | |
5589 | 101 return; |
102 endif | |
103 | |
104 | |
105 if (n > 0) | |
106 doidx = n; | |
107 else | |
6494 | 108 doidx = 1:length(idx)-1; |
5589 | 109 endif |
6494 | 110 for i = 1:length (doidx) |
5589 | 111 ## Pause between demos |
112 if (i > 1) | |
6494 | 113 input ("Press <enter> to continue: ", "s"); |
5589 | 114 endif |
115 | |
116 ## Process each demo without failing | |
117 try | |
6494 | 118 block = code(idx(doidx(i)):idx(doidx(i)+1)-1); |
5589 | 119 ## 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
|
120 eval (cstrcat ("function __demo__()\n", block, "\nendfunction")); |
5589 | 121 ## Display the code that will be executed before executing it |
6494 | 122 printf ("%s example %d:%s\n\n", name, doidx(i), block); |
5589 | 123 __demo__; |
124 catch | |
125 ## 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
|
126 printf ("%s example %d: failed\n%s\n", name, doidx(i), __error_text__); |
5589 | 127 end_try_catch |
128 clear __demo__; | |
129 endfor | |
130 | |
131 endfunction | |
132 | |
133 %!demo | |
134 %! t=0:0.01:2*pi; x = sin(t); | |
135 %! plot(t,x) | |
136 %! %------------------------------------------------- | |
137 %! % the figure window shows one cycle of a sine wave |