changelog shortlog tags changeset files revisions annotate raw

scripts/testfun/demo.m

changeset 10289: 4b124317dc38
parent:eb63fbe60fab
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (57 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2000, 2006, 2007, 2008, 2009 Paul Kienzle
2##
3## This file is part of Octave.
4##
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.
14##
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
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## @group
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
37## @end group
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
45## variables. This means that all demos in your function must use
46## separate initialization code. Alternatively, you can combine your
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"
62## with the 'if' on the same line as 'demo'. For example,
63##
64## @example
65## @group
66## %!demo if 1
67## %! function y=f(x)
68## %! y=x;
69## %! endfunction
70## %! f(3)
71## %! endif
72## @end group
73## @end example
74## @seealso{test, example}
75## @end deftypefn
76
77## FIXME: modify subplot so that gnuplot_has_multiplot == 0 causes it to
78## use the current figure window but pause if not plotting in the
79## first subplot.
80
81function demo (name, n)
82
83 if (nargin < 1 || nargin > 2)
84 print_usage ();
85 endif
86
87 if (nargin < 2)
88 n = 0;
89 endif
90
91 [code, idx] = test (name, "grabdemo");
92 if (length (idx) == 0)
93 warning ("demo not available for %s", name);
94 return;
95 elseif (n >= length (idx))
96 warning ("only %d demos available for %s", length (idx) - 1, name);
97 return;
98 endif
99
100
101 if (n > 0)
102 doidx = n;
103 else
104 doidx = 1:length(idx)-1;
105 endif
106 for i = 1:length (doidx)
107 ## Pause between demos
108 if (i > 1)
109 input ("Press <enter> to continue: ", "s");
110 endif
111
112 ## Process each demo without failing
113 try
114 block = code(idx(doidx(i)):idx(doidx(i)+1)-1);
115 ## Use an environment without variables
116 eval (cstrcat ("function __demo__()\n", block, "\nendfunction"));
117 ## Display the code that will be executed before executing it
118 printf ("%s example %d:%s\n\n", name, doidx(i), block);
119 __demo__;
120 catch
121 ## Let the programmer know which demo failed.
122 printf ("%s example %d: failed\n%s", name, doidx(i), __error_text__);
123 end_try_catch
124 clear __demo__;
125 endfor
126
127endfunction
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