7017
|
1 ## Copyright (C) 2000, 2006, 2007 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 |
|
28 ## arbitrary octave code. For example: |
|
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 |
|
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 |
6494
|
79 function demo (name, n) |
5589
|
80 |
|
81 if (nargin < 1 || nargin > 2) |
6046
|
82 print_usage (); |
5589
|
83 endif |
|
84 |
|
85 if (nargin < 2) |
|
86 n = 0; |
|
87 endif |
|
88 |
6494
|
89 [code, idx] = test (name, "grabdemo"); |
|
90 if (length (idx) == 0) |
|
91 warning ("demo not available for %s", name); |
5589
|
92 return; |
6494
|
93 elseif (n >= length (idx)) |
|
94 warning ("only %d demos available for %s", length (idx) - 1, name); |
5589
|
95 return; |
|
96 endif |
|
97 |
|
98 |
|
99 if (n > 0) |
|
100 doidx = n; |
|
101 else |
6494
|
102 doidx = 1:length(idx)-1; |
5589
|
103 endif |
6494
|
104 for i = 1:length (doidx) |
5589
|
105 ## Pause between demos |
|
106 if (i > 1) |
6494
|
107 input ("Press <enter> to continue: ", "s"); |
5589
|
108 endif |
|
109 |
|
110 ## Process each demo without failing |
|
111 try |
6494
|
112 block = code(idx(doidx(i)):idx(doidx(i)+1)-1); |
5589
|
113 ## Use an environment without variables |
6494
|
114 eval (strcat ("function __demo__()\n", block, "\nendfunction")); |
5589
|
115 ## Display the code that will be executed before executing it |
6494
|
116 printf ("%s example %d:%s\n\n", name, doidx(i), block); |
5589
|
117 __demo__; |
|
118 catch |
|
119 ## Let the programmer know which demo failed. |
6494
|
120 printf ("%s example %d: failed\n%s", name, doidx(i), __error_text__); |
5589
|
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 |