Mercurial > hg > octave-nkf
annotate scripts/testfun/demo.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 5dd06f19e9be |
children | 1bf0ce0930be |
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 | |
25 ## from the usual script code, all lines are prefixed by @code{%!}. Each | |
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 | |
8481
00df69d7e698
[docs] capitalize Octave consistently
Brian Gough <bjg@gnu.org>
parents:
8202
diff
changeset
|
28 ## arbitrary Octave code. For example: |
5589 | 29 ## |
30 ## @example | |
31 ## %!demo | |
32 ## %! t=0:0.01:2*pi; x = sin(t); | |
33 ## %! plot(t,x) | |
34 ## %! %------------------------------------------------- | |
35 ## %! % the figure window shows one cycle of a sine wave | |
36 ## @end example | |
37 ## | |
38 ## Note that the code is displayed before it is executed, so a simple | |
39 ## comment at the end suffices. It is generally not necessary to use | |
40 ## disp or printf within the demo. | |
41 ## | |
42 ## Demos are run in a function environment with no access to external | |
43 ## variables. This means that all demos in your function must use | |
44 ## separate initialization code. Alternatively, you can combine your | |
45 ## demos into one huge demo, with the code: | |
46 ## | |
47 ## @example | |
48 ## %! input("Press <enter> to continue: ","s"); | |
49 ## @end example | |
50 ## | |
51 ## between the sections, but this is discouraged. Other techniques | |
52 ## include using multiple plots by saying figure between each, or | |
53 ## using subplot to put multiple plots in the same window. | |
54 ## | |
55 ## Also, since demo evaluates inside a function context, you cannot | |
56 ## define new functions inside a demo. Instead you will have to | |
57 ## use @code{eval(example('function',n))} to see them. Because eval only | |
58 ## evaluates one line, or one statement if the statement crosses | |
59 ## multiple lines, you must wrap your demo in "if 1 <demo stuff> endif" | |
60 ## with the 'if' on the same line as 'demo'. For example, | |
61 ## | |
62 ## @example | |
63 ## %!demo if 1 | |
64 ## %! function y=f(x) | |
65 ## %! y=x; | |
66 ## %! endfunction | |
67 ## %! f(3) | |
68 ## %! endif | |
69 ## @end example | |
5642 | 70 ## @seealso{test, example} |
5589 | 71 ## @end deftypefn |
72 | |
8202
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7540
diff
changeset
|
73 ## 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
|
74 ## 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
|
75 ## first subplot. |
5589 | 76 |
6494 | 77 function demo (name, n) |
5589 | 78 |
79 if (nargin < 1 || nargin > 2) | |
6046 | 80 print_usage (); |
5589 | 81 endif |
82 | |
83 if (nargin < 2) | |
84 n = 0; | |
85 endif | |
86 | |
6494 | 87 [code, idx] = test (name, "grabdemo"); |
88 if (length (idx) == 0) | |
89 warning ("demo not available for %s", name); | |
5589 | 90 return; |
6494 | 91 elseif (n >= length (idx)) |
92 warning ("only %d demos available for %s", length (idx) - 1, name); | |
5589 | 93 return; |
94 endif | |
95 | |
96 | |
97 if (n > 0) | |
98 doidx = n; | |
99 else | |
6494 | 100 doidx = 1:length(idx)-1; |
5589 | 101 endif |
6494 | 102 for i = 1:length (doidx) |
5589 | 103 ## Pause between demos |
104 if (i > 1) | |
6494 | 105 input ("Press <enter> to continue: ", "s"); |
5589 | 106 endif |
107 | |
108 ## Process each demo without failing | |
109 try | |
6494 | 110 block = code(idx(doidx(i)):idx(doidx(i)+1)-1); |
5589 | 111 ## 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
|
112 eval (cstrcat ("function __demo__()\n", block, "\nendfunction")); |
5589 | 113 ## Display the code that will be executed before executing it |
6494 | 114 printf ("%s example %d:%s\n\n", name, doidx(i), block); |
5589 | 115 __demo__; |
116 catch | |
117 ## Let the programmer know which demo failed. | |
6494 | 118 printf ("%s example %d: failed\n%s", name, doidx(i), __error_text__); |
5589 | 119 end_try_catch |
120 clear __demo__; | |
121 endfor | |
122 | |
123 endfunction | |
124 | |
125 %!demo | |
126 %! t=0:0.01:2*pi; x = sin(t); | |
127 %! plot(t,x) | |
128 %! %------------------------------------------------- | |
129 %! % the figure window shows one cycle of a sine wave |