Mercurial > octave
annotate scripts/testfun/demo.m @ 33658:b1d6e40ac737 stable tip
NEWS.9.md: Fix typo and minor formatting changes.
* etc/NEWS.9.md: Fix typo. Adjust whitespace. Use markdown syntax for code
snippets.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Wed, 05 Jun 2024 11:27:35 +0200 |
parents | 2e484f9f1f18 |
children |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
32632
2e484f9f1f18
maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
31706
diff
changeset
|
3 ## Copyright (C) 2000-2024 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
5589 | 7 ## |
7016 | 8 ## This file is part of Octave. |
5589 | 9 ## |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
7016 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
7016 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
5589 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
5589 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
27 ## @deftypefn {} {} demo @var{name} |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
28 ## @deftypefnx {} {} demo @var{name} @var{n} |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
29 ## @deftypefnx {} {} demo ("@var{name}") |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20162
diff
changeset
|
30 ## @deftypefnx {} {} demo ("@var{name}", @var{n}) |
5589 | 31 ## |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
32 ## Run example code block @var{n} associated with the function @var{name}. |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
33 ## |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
34 ## If @var{n} is not specified, all examples are run. |
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
35 ## |
19023 | 36 ## The preferred location for example code blocks is embedded within the script |
37 ## m-file immediately following the code that it exercises. Alternatively, | |
38 ## the examples may be stored in a file with the same name but no extension | |
39 ## located on Octave's load path. To separate examples from regular script | |
40 ## code all lines are prefixed by @code{%!}. Each example must also be | |
41 ## introduced by the keyword @qcode{"demo"} flush left to the prefix with no | |
42 ## intervening spaces. The remainder of the example can contain arbitrary | |
43 ## Octave code. For example: | |
5589 | 44 ## |
45 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
46 ## @group |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
47 ## %!demo |
14335
ce2b59a6d0e5
maint: periodic merge of stable to default.
Rik <octave@nomad.inbox5.com>
diff
changeset
|
48 ## %! t = 0:0.01:2*pi; |
ce2b59a6d0e5
maint: periodic merge of stable to default.
Rik <octave@nomad.inbox5.com>
diff
changeset
|
49 ## %! x = sin (t); |
ce2b59a6d0e5
maint: periodic merge of stable to default.
Rik <octave@nomad.inbox5.com>
diff
changeset
|
50 ## %! plot (t, x); |
19023 | 51 ## %! title ("one cycle of a sine wave"); |
52 ## %! #------------------------------------------------- | |
53 ## %! # 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
|
54 ## @end group |
5589 | 55 ## @end example |
56 ## | |
19023 | 57 ## Note that the code is displayed before it is executed so that a simple |
58 ## comment at the end suffices for labeling what is being shown. For plots, | |
59 ## labeling can also be done with @code{title} or @code{text}. It is generally | |
60 ## @strong{not} necessary to use @code{disp} or @code{printf} within the demo. | |
5589 | 61 ## |
19023 | 62 ## Demos are run in a stand-alone function environment with no access to |
63 ## external variables. This means that every demo must have separate | |
64 ## initialization code. Alternatively, all demos can be combined into a single | |
65 ## large demo with the code | |
5589 | 66 ## |
67 ## @example | |
19023 | 68 ## %! input ("Press <enter> to continue: ", "s"); |
5589 | 69 ## @end example |
70 ## | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
71 ## @noindent |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
72 ## between the sections, but this usage is discouraged. Other techniques to |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
73 ## avoid multiple initialization blocks include using multiple plots with a new |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
74 ## @code{figure} command between each plot, or using @code{subplot} to put |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
75 ## multiple plots in the same window. |
5589 | 76 ## |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
77 ## Finally, because @code{demo} evaluates within a function context it is not |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
78 ## possible to define new functions within the code. Anonymous functions make |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
79 ## a good substitute in most instances. If function blocks @strong{must} be |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
80 ## used then the code @code{eval (example ("function", n))} will allow Octave |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
81 ## to see them. This has its own problems, however, as @code{eval} only |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
82 ## evaluates one line or statement at a time. In this case the function |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
83 ## declaration must be wrapped with @qcode{"if 1 <demo stuff> endif"} where |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
84 ## @qcode{"if"} is on the same line as @qcode{"demo"}. For example: |
5589 | 85 ## |
86 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
87 ## @group |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 ## %!demo if 1 |
19023 | 89 ## %! function y = f(x) |
90 ## %! y = x; | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
91 ## %! endfunction |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
92 ## %! f(3) |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
93 ## %! endif |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
94 ## @end group |
5589 | 95 ## @end example |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
96 ## |
19023 | 97 ## @seealso{rundemos, example, test} |
5589 | 98 ## @end deftypefn |
99 | |
8202
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7540
diff
changeset
|
100 ## 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
|
101 ## 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
|
102 ## first subplot. |
5589 | 103 |
19023 | 104 function demo (name, n = 0) |
5589 | 105 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
106 if (nargin < 1) |
6046 | 107 print_usage (); |
5589 | 108 endif |
109 | |
19023 | 110 if (ischar (n)) |
12505
6a1fe83fe129
Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
111 n = str2double (n); |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
13065
diff
changeset
|
112 endif |
5589 | 113 |
19023 | 114 if (! (isreal (n) && isscalar (n) && n == fix (n))) |
115 error ("demo: N must be a scalar integer"); | |
116 endif | |
117 | |
21997
d5feadcf2949
Turn pager off during demos, to work around a Qt hang (bug #48296)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21758
diff
changeset
|
118 ## Paging causes Qt graphics to hang on some demos. |
d5feadcf2949
Turn pager off during demos, to work around a Qt hang (bug #48296)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21758
diff
changeset
|
119 page_screen_output (false, "local"); |
d5feadcf2949
Turn pager off during demos, to work around a Qt hang (bug #48296)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21758
diff
changeset
|
120 |
6494 | 121 [code, idx] = test (name, "grabdemo"); |
15520
bcace51598ed
Improve demo() to warn when an input function does not exist.
Rik <rik@octave.org>
parents:
15079
diff
changeset
|
122 |
bcace51598ed
Improve demo() to warn when an input function does not exist.
Rik <rik@octave.org>
parents:
15079
diff
changeset
|
123 if (idx == -1) |
19023 | 124 warning ("demo: no function %s found", name); |
15520
bcace51598ed
Improve demo() to warn when an input function does not exist.
Rik <rik@octave.org>
parents:
15079
diff
changeset
|
125 return; |
bcace51598ed
Improve demo() to warn when an input function does not exist.
Rik <rik@octave.org>
parents:
15079
diff
changeset
|
126 elseif (isempty (idx)) |
19023 | 127 warning ("demo: no demo available for %s", name); |
5589 | 128 return; |
6494 | 129 elseif (n >= length (idx)) |
19023 | 130 warning ("demo: only %d demos available for %s", length (idx) - 1, name); |
5589 | 131 return; |
132 endif | |
133 | |
134 if (n > 0) | |
135 doidx = n; | |
136 else | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
137 doidx = 1:(length (idx) - 1); |
5589 | 138 endif |
21657
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
139 clear_figures (); |
6494 | 140 for i = 1:length (doidx) |
5589 | 141 ## Pause between demos |
142 if (i > 1) | |
6494 | 143 input ("Press <enter> to continue: ", "s"); |
21657
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
144 clear_figures (); |
5589 | 145 endif |
146 | |
147 ## Process each demo without failing | |
148 try | |
6494 | 149 block = code(idx(doidx(i)):idx(doidx(i)+1)-1); |
15079
dda73cb60ac5
demo.m: Allow functions in demo blocks (bug #33981).
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
150 ## Use an environment without variables |
19023 | 151 eval (["function __demo__ ()\n" block "\nendfunction"]); |
15079
dda73cb60ac5
demo.m: Allow functions in demo blocks (bug #33981).
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
152 ## Display the code that will be executed before executing it |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
153 printf ("%s example %d:%s\n\n", name, doidx(i), block); |
15079
dda73cb60ac5
demo.m: Allow functions in demo blocks (bug #33981).
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
154 __demo__; |
5589 | 155 catch |
156 ## Let the programmer know which demo failed. | |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
157 printf ("%s example %d: failed\n%s\n", name, doidx(i), lasterr ()); |
5589 | 158 end_try_catch |
159 clear __demo__; | |
160 endfor | |
161 | |
162 endfunction | |
163 | |
21657
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
164 function clear_figures () |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
21657
diff
changeset
|
165 |
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
21657
diff
changeset
|
166 ## Prevent proliferation of figure windows. If any figure windows exist, |
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
21657
diff
changeset
|
167 ## close all but one and clear the one remaining. |
21657
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
168 figs = __go_figure_handles__ (); |
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
169 if (! isempty (figs)) |
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
170 if (numel (figs) > 1) |
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
171 figs = sort (figs); |
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
172 figs(1) = []; |
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
173 close (figs); |
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
174 endif |
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
175 clf ("reset"); |
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
176 endif |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
21657
diff
changeset
|
177 |
21657
b45d53d1d638
prevent proliferation of figure windows when running demos
John W. Eaton <jwe@octave.org>
parents:
21634
diff
changeset
|
178 endfunction |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
179 |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
21657
diff
changeset
|
180 |
5589 | 181 %!demo |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
182 %! t = 0:0.01:2*pi; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
183 %! x = sin (t); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
184 %! plot (t, x); |
19023 | 185 %! title ("one cycle of a sine wave"); |
186 %! #------------------------------------------------- | |
187 %! # the figure window shows one cycle of a sine wave | |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
12687
diff
changeset
|
188 |
28896
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
189 %!error <Invalid call> demo () |
19023 | 190 %!error <N must be a scalar integer> demo ("demo", {1}) |
191 %!error <N must be a scalar integer> demo ("demo", ones (2,2)) | |
192 %!error <N must be a scalar integer> demo ("demo", 1.5) | |
193 %!warning <no function .* found> demo ("_%NOT_A_FUNCTION%_"); | |
194 %!warning <only 1 demos available for demo> demo ("demo", 10); |