Mercurial > octave
annotate scripts/testfun/example.m @ 30875:5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
For new users of Octave it is best to show explicit calling forms
in the documentation and to show a return argument when it exists.
* bp-table.cc, shift.m, accumarray.m, accumdim.m, bincoeff.m, bitcmp.m,
bitget.m, bitset.m, blkdiag.m, celldisp.m, cplxpair.m, dblquad.m, flip.m,
fliplr.m, flipud.m, idivide.m, int2str.m, interpft.m, logspace.m, num2str.m,
polyarea.m, postpad.m, prepad.m, randi.m, repmat.m, rng.m, rot90.m, rotdim.m,
structfun.m, triplequad.m, uibuttongroup.m, uicontrol.m, uipanel.m,
uipushtool.m, uitoggletool.m, uitoolbar.m, waitforbuttonpress.m, help.m,
__additional_help_message__.m, hsv.m, im2double.m, im2frame.m, javachk.m,
usejava.m, argnames.m, char.m, formula.m, inline.m, __vectorize__.m, findstr.m,
flipdim.m, strmatch.m, vectorize.m, commutation_matrix.m, cond.m, cross.m,
duplication_matrix.m, expm.m, orth.m, rank.m, rref.m, trace.m, vech.m, cast.m,
compare_versions.m, delete.m, dir.m, fileattrib.m, grabcode.m, gunzip.m,
inputname.m, license.m, list_primes.m, ls.m, mexext.m, movefile.m,
namelengthmax.m, nargoutchk.m, nthargout.m, substruct.m, swapbytes.m, ver.m,
verLessThan.m, what.m, fminunc.m, fsolve.m, fzero.m, optimget.m, __fdjac__.m,
matlabroot.m, savepath.m, campos.m, camroll.m, camtarget.m, camup.m, camva.m,
camzoom.m, clabel.m, diffuse.m, legend.m, orient.m, rticks.m, specular.m,
thetaticks.m, xlim.m, xtickangle.m, xticklabels.m, xticks.m, ylim.m,
ytickangle.m, yticklabels.m, yticks.m, zlim.m, ztickangle.m, zticklabels.m,
zticks.m, ellipsoid.m, isocolors.m, isonormals.m, stairs.m, surfnorm.m,
__actual_axis_position__.m, __pltopt__.m, close.m, graphics_toolkit.m, pan.m,
print.m, printd.m, __ghostscript__.m, __gnuplot_print__.m, __opengl_print__.m,
rotate3d.m, subplot.m, zoom.m, compan.m, conv.m, poly.m, polyaffine.m,
polyder.m, polyint.m, polyout.m, polyreduce.m, polyvalm.m, roots.m, prefdir.m,
prefsfile.m, profexplore.m, profexport.m, profshow.m, powerset.m, unique.m,
arch_rnd.m, arma_rnd.m, autoreg_matrix.m, bartlett.m, blackman.m, detrend.m,
durbinlevinson.m, fftconv.m, fftfilt.m, fftshift.m, fractdiff.m, hamming.m,
hanning.m, hurst.m, ifftshift.m, rectangle_lw.m, rectangle_sw.m, triangle_lw.m,
sinc.m, sinetone.m, sinewave.m, spectral_adf.m, spectral_xdf.m, spencer.m,
ilu.m, __sprand__.m, sprand.m, sprandn.m, sprandsym.m, treelayout.m, beta.m,
betainc.m, betaincinv.m, betaln.m, cosint.m, expint.m, factorial.m, gammainc.m,
gammaincinv.m, lcm.m, nthroot.m, perms.m, reallog.m, realpow.m, realsqrt.m,
sinint.m, hadamard.m, hankel.m, hilb.m, invhilb.m, magic.m, pascal.m, rosser.m,
toeplitz.m, vander.m, wilkinson.m, center.m, corr.m, cov.m, discrete_cdf.m,
discrete_inv.m, discrete_pdf.m, discrete_rnd.m, empirical_cdf.m,
empirical_inv.m, empirical_pdf.m, empirical_rnd.m, kendall.m, kurtosis.m,
mad.m, mean.m, meansq.m, median.m, mode.m, moment.m, range.m, ranks.m,
run_count.m, skewness.m, spearman.m, statistics.m, std.m, base2dec.m,
bin2dec.m, blanks.m, cstrcat.m, deblank.m, dec2base.m, dec2bin.m, dec2hex.m,
hex2dec.m, index.m, regexptranslate.m, rindex.m, strcat.m, strjust.m,
strtrim.m, strtrunc.m, substr.m, untabify.m, __have_feature__.m,
__prog_output_assert__.m, __run_test_suite__.m, example.m, fail.m, asctime.m,
calendar.m, ctime.m, date.m, etime.m:
Add return arguments to @deftypefn macros where they were missing. Rename
variables in functions (particularly generic "retval") to match documentation.
Rename some return variables for (hopefully) better clarity (e.g., 'ax' to 'hax'
to indicate it is a graphics handle to an axes object).
author | Rik <rik@octave.org> |
---|---|
date | Wed, 30 Mar 2022 20:40:27 -0700 |
parents | 796f54d4ddbf |
children | 597f3ee61a48 |
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 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
3 ## Copyright (C) 2000-2022 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 {} {} example @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 {} {} example @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 {} {} example ("@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 {} {} example ("@var{name}", @var{n}) |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
31 ## @deftypefnx {} {[@var{codestr}, @var{codeidx}] =} example (@dots{}) |
5589 | 32 ## |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
33 ## Display the code for example @var{n} associated with the function |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
34 ## @var{name}, but do not run it. |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
35 ## |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
36 ## If @var{n} is not specified, all examples are displayed. |
5589 | 37 ## |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
38 ## When called with output arguments, the examples are returned in the form of |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
39 ## a string @var{codestr}, with @var{codeidx} indicating the ending position of |
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
40 ## the various examples. |
5589 | 41 ## |
28958
6fd6ad758b10
doc: Use @xref, @pxref rather than "see @code{}" in TexInfo.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
42 ## For a complete explanation @pxref{XREFdemo,,@code{demo}}. |
5642 | 43 ## @seealso{demo, test} |
5589 | 44 ## @end deftypefn |
45 | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
46 function [codestr, codeidx] = example (name, n = 0) |
5589 | 47 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
48 if (nargin < 1) |
6046 | 49 print_usage (); |
5589 | 50 endif |
12505
6a1fe83fe129
Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
51 |
19014
90a541a12c4b
example.m: Use ex_code, ex_idx for output variable names.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
52 if (ischar (n)) |
12505
6a1fe83fe129
Allow command forms of example and demo.
Ben Abbott <bpabbott@mac.com>
parents:
11587
diff
changeset
|
53 n = str2double (n); |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12643
diff
changeset
|
54 endif |
5589 | 55 |
19024
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
56 if (! (isreal (n) && isscalar (n) && n == fix (n))) |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
57 error ("example: N must be a scalar integer"); |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
58 endif |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
59 |
6494 | 60 [code, idx] = test (name, "grabdemo"); |
19024
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
61 |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
62 if (idx == -1) |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
63 warning ("example: no function %s found", name); |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
64 return; |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
65 elseif (isempty (idx)) |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
66 warning ("example: no example available for %s", name); |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
67 return; |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
68 elseif (n >= length (idx)) |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
69 warning ("example: only %d examples available for %s", |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
70 length (idx) - 1, name); |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
71 return; |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
72 endif |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
73 |
5589 | 74 if (nargout > 0) |
75 if (n > 0) | |
6494 | 76 if (n <= length (idx)) |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
77 codestr = code(idx(n):idx(n+1)-1); |
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
78 codeidx = [1, length(codestr)+1]; |
5589 | 79 else |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
80 codestr = ""; |
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
81 codeidx = []; |
5589 | 82 endif |
83 else | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
84 codestr = code; |
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
85 codeidx = idx; |
5589 | 86 endif |
87 else | |
88 if (n > 0) | |
89 doidx = n; | |
90 else | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
91 doidx = 1:length (idx) - 1; |
5589 | 92 endif |
93 | |
8507 | 94 for i = 1:length (doidx) |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
95 block = code(idx(doidx(i)):idx(doidx(i)+1)-1); |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
96 printf ("%s example %d:%s\n\n", name, doidx(i), block); |
5589 | 97 endfor |
98 endif | |
99 | |
100 endfunction | |
101 | |
14039
e98140f84ae0
test: Rewrite %!tests to preserve warning state.
Rik <octave@nomad.inbox5.com>
parents:
13141
diff
changeset
|
102 |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
103 ## WARNING: don't modify the demos without modifying the tests! |
5589 | 104 %!demo |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
105 %! example ("example"); |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
106 |
5589 | 107 %!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
|
108 %! clf; |
19024
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
109 %! t = 0:0.01:2*pi; |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
110 %! x = sin (t); |
12643
55430618bd5f
Properly warn when demo or example called on a function without demos
Rik <octave@nomad.inbox5.com>
parents:
12505
diff
changeset
|
111 %! plot (t,x) |
5589 | 112 |
21317
a4faec57f4c8
maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
113 %!assert (example ("example", 1), "\n example (\"example\");") |
19024
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
114 |
5589 | 115 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
116 %! [code, idx] = example ("example"); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
117 %! assert (code, ... |
19024
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
118 %! "\n example (\"example\");\n clf;\n t = 0:0.01:2*pi;\n x = sin (t);\n plot (t,x)"); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
119 %! assert (idx, [1, 23, 73]); |
5589 | 120 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
121 ## Test input validation |
28896
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
122 %!error <Invalid call> example () |
19024
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
123 %!error <N must be a scalar integer> example ("example", {1}) |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
124 %!error <N must be a scalar integer> example ("example", ones (2,2)) |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
125 %!error <N must be a scalar integer> example ("example", 1.5) |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
126 %!warning <no function .* found> example ("_%NOT_A_FUNCTION%_"); |
26a770330953
example.m: Add better input validation and %!error tests for that.
Rik <rik@octave.org>
parents:
19014
diff
changeset
|
127 %!warning <only 2 examples available for example> example ("example", 10); |