1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
########################################################################
##
## Copyright (C) 2010-2024 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING. If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################
## -*- texinfo -*-
## @deftypefn {} {} dump_demos ()
## @deftypefnx {} {} dump_demos (@var{dirs})
## @deftypefnx {} {} dump_demos (@var{dirs}, @var{mfile})
## @deftypefnx {} {} dump_demos (@var{dirs}, @var{mfile}, @var{fmt})
## Produce a script, with the name specified by @var{mfile}, containing
## the demos in the directories, @var{dirs}. The demos are assumed to produce
## graphical output, whose renderings are saved with the specified format,
## @var{fmt}.
##
## The defaults for each input are;
##
## @table @var
## @item @var{dirs}
## @code{@{"plot/appearance", "plot/draw", "plot/util", "image"@}}
##
## @item @var{mfile}
## @qcode{"dump.m"}
##
## @item @var{fmt}
## @qcode{"png"}
## @end table
##
## For example, to produce PNG output for all demos of the functions
## in the plot directory;
##
## @example
## dump_demos plot dump.m png
## @end example
## @seealso{fntests, test, demo}
## @end deftypefn
function dump_demos (dirs={"plot/appearance", "plot/draw", "plot/util", "image"}, mfile="dump_plot_demos.m", fmt="png")
if (ischar (dirs))
dirs = {dirs};
elseif (! iscellstr (dirs))
error ("dump_demos: DIRS must be a cell array of strings with directory names");
endif
if (! isunix ())
dirs = strrep (dirs, "/", filesep ());
endif
[~, funcname, ext] = fileparts (mfile);
if (isempty (ext))
mfile = [mfile ".m"];
endif
## Create script beginning (close figures, etc.)
fid = fopen (mfile, "w");
fprintf (fid, "%% DO NOT EDIT! Generated automatically by dump_demos.m\n");
fprintf (fid, "function %s ()\n", funcname);
fprintf (fid, "set (0, 'DefaultAxesColorOrder', ...\n");
fprintf (fid, " [ 0.00000 0.44700 0.74100 ;\n");
fprintf (fid, " 0.85000 0.32500 0.09800 ;\n");
fprintf (fid, " 0.92900 0.69400 0.12500 ;\n");
fprintf (fid, " 0.49400 0.18400 0.55600 ;\n");
fprintf (fid, " 0.46600 0.67400 0.18800 ;\n");
fprintf (fid, " 0.30100 0.74500 0.93300 ;\n");
fprintf (fid, " 0.63500 0.07800 0.18400 ]);\n");
fprintf (fid, "set (0, 'DefaultFigureColorMap', ...\n");
fprintf (fid, " [ 0.2670040 0.0048743 0.3294152 ;\n");
fprintf (fid, " 0.2726517 0.0258457 0.3533673 ;\n");
fprintf (fid, " 0.2771063 0.0509139 0.3762361 ;\n");
fprintf (fid, " 0.2803562 0.0742015 0.3979015 ;\n");
fprintf (fid, " 0.2823900 0.0959536 0.4182508 ;\n");
fprintf (fid, " 0.2832046 0.1168933 0.4371789 ;\n");
fprintf (fid, " 0.2828093 0.1373502 0.4545959 ;\n");
fprintf (fid, " 0.2812308 0.1574799 0.4704339 ;\n");
fprintf (fid, " 0.2785162 0.1773480 0.4846539 ;\n");
fprintf (fid, " 0.2747355 0.1969692 0.4972505 ;\n");
fprintf (fid, " 0.2699818 0.2163303 0.5082545 ;\n");
fprintf (fid, " 0.2643686 0.2354047 0.5177319 ;\n");
fprintf (fid, " 0.2580262 0.2541617 0.5257802 ;\n");
fprintf (fid, " 0.2510987 0.2725732 0.5325222 ;\n");
fprintf (fid, " 0.2437329 0.2906195 0.5380971 ;\n");
fprintf (fid, " 0.2360733 0.3082910 0.5426518 ;\n");
fprintf (fid, " 0.2282632 0.3255865 0.5463354 ;\n");
fprintf (fid, " 0.2204250 0.3425172 0.5492871 ;\n");
fprintf (fid, " 0.2126666 0.3591022 0.5516350 ;\n");
fprintf (fid, " 0.2050791 0.3753661 0.5534932 ;\n");
fprintf (fid, " 0.1977219 0.3913409 0.5549535 ;\n");
fprintf (fid, " 0.1906314 0.4070615 0.5560891 ;\n");
fprintf (fid, " 0.1838194 0.4225638 0.5569522 ;\n");
fprintf (fid, " 0.1772724 0.4378855 0.5575761 ;\n");
fprintf (fid, " 0.1709575 0.4530630 0.5579740 ;\n");
fprintf (fid, " 0.1648329 0.4681295 0.5581427 ;\n");
fprintf (fid, " 0.1588454 0.4831171 0.5580587 ;\n");
fprintf (fid, " 0.1529512 0.4980530 0.5576847 ;\n");
fprintf (fid, " 0.1471316 0.5129595 0.5569733 ;\n");
fprintf (fid, " 0.1414022 0.5278543 0.5558645 ;\n");
fprintf (fid, " 0.1358330 0.5427501 0.5542887 ;\n");
fprintf (fid, " 0.1305821 0.5576525 0.5521757 ;\n");
fprintf (fid, " 0.1258984 0.5725631 0.5494454 ;\n");
fprintf (fid, " 0.1221631 0.5874763 0.5460234 ;\n");
fprintf (fid, " 0.1198724 0.6023824 0.5418306 ;\n");
fprintf (fid, " 0.1196266 0.6172658 0.5367956 ;\n");
fprintf (fid, " 0.1220459 0.6321070 0.5308480 ;\n");
fprintf (fid, " 0.1276677 0.6468818 0.5239242 ;\n");
fprintf (fid, " 0.1368349 0.6615629 0.5159668 ;\n");
fprintf (fid, " 0.1496433 0.6761197 0.5069243 ;\n");
fprintf (fid, " 0.1659673 0.6905190 0.4967519 ;\n");
fprintf (fid, " 0.1855384 0.7047252 0.4854121 ;\n");
fprintf (fid, " 0.2080305 0.7187010 0.4728733 ;\n");
fprintf (fid, " 0.2331273 0.7324064 0.4591059 ;\n");
fprintf (fid, " 0.2605315 0.7458020 0.4440959 ;\n");
fprintf (fid, " 0.2900007 0.7588465 0.4278259 ;\n");
fprintf (fid, " 0.3213300 0.7714979 0.4102927 ;\n");
fprintf (fid, " 0.3543553 0.7837140 0.3914876 ;\n");
fprintf (fid, " 0.3889303 0.7954531 0.3714207 ;\n");
fprintf (fid, " 0.4249331 0.8066739 0.3500988 ;\n");
fprintf (fid, " 0.4622468 0.8173376 0.3275447 ;\n");
fprintf (fid, " 0.5007536 0.8274091 0.3037990 ;\n");
fprintf (fid, " 0.5403370 0.8368582 0.2789167 ;\n");
fprintf (fid, " 0.5808612 0.8456634 0.2530009 ;\n");
fprintf (fid, " 0.6221708 0.8538156 0.2262237 ;\n");
fprintf (fid, " 0.6640873 0.8613210 0.1988794 ;\n");
fprintf (fid, " 0.7064038 0.8682063 0.1714949 ;\n");
fprintf (fid, " 0.7488853 0.8745222 0.1450376 ;\n");
fprintf (fid, " 0.7912731 0.8803462 0.1212910 ;\n");
fprintf (fid, " 0.8333021 0.8857801 0.1033262 ;\n");
fprintf (fid, " 0.8747175 0.8909453 0.0953508 ;\n");
fprintf (fid, " 0.9152963 0.8959735 0.1004700 ;\n");
fprintf (fid, " 0.9548396 0.9010058 0.1178764 ;\n");
fprintf (fid, " 0.9932479 0.9061566 0.1439362 ]);\n");
fprintf (fid, "close all\n");
fprintf (fid, "more off\n");
fprintf (fid, "diary diary.log\n");
## Run and print the demos in each directory
for i = 1:numel (dirs)
d = dirs{i};
if (! is_absolute_filename (d))
d = dir_in_loadpath (d);
endif
if (! isfolder (d))
error ("dump_demos: directory %s does not exist", d);
endif
dump_all_demos (d, fid, fmt);
endfor
## Add helper functions like sombrero
dump_helper_fcns (fid);
## Stop and flush diary
fprintf (fid, "\ndiary off\n");
## Create script ending
fprintf (fid, "end\n\n");
## Close script
fclose (fid);
endfunction
function dump_all_demos (directory, fid, fmt)
dirinfo = dir (fullfile (directory, "*.m"));
flist = {dirinfo.name};
## Remove uigetdir, uigetfile, uiputfile, etc.
flist = flist(! strncmp (flist, "ui", 2));
## Remove linkaxes, linkprops
flist = flist(! strncmp (flist, "link", 4));
## Remove colormap
flist = flist(! strncmp (flist, "colormap", 8));
for i = 1:numel (flist)
fcn = flist{i};
fcn(end-1:end) = []; # remove .m
demos = get_demos (fcn);
for d = 1:numel (demos)
idx = sprintf ("%02d", d);
base_fcn = sprintf ("%s_%s", fcn, idx);
fn = sprintf ('%s.%s', base_fcn, fmt);
## Wrap each demo in a function which create a local scope
## to prevent that a previous demo overwrites i or pi, for example
fprintf (fid, "\nfunction %s ()\n", base_fcn);
fprintf (fid, " %s\n\n", demos{d});
fprintf (fid, "end\n\n");
fprintf (fid, "try\n");
## First check if the file already exists, skip demo if found
fprintf (fid, " if (~ exist ('%s', 'file'))\n", fn);
## Invoke the ancient, deprecated random seed generators, but there is an
## initialization mismatch with the more modern generators reported
## here (https://savannah.gnu.org/bugs/?42557).
fprintf (fid, " rand ('seed', 1);\n");
fprintf (fid, " tic ();\n");
fprintf (fid, " %s ();\n", base_fcn);
fprintf (fid, " t_plot = toc ();\n");
fprintf (fid, " fig = (get (0, 'currentfigure'));\n");
fprintf (fid, " if (~ isempty (fig))\n");
fprintf (fid, " figure (fig);\n");
fprintf (fid, " fprintf ('Printing ""%s"" ... ');\n", fn);
fprintf (fid, " tic ();\n");
fprintf (fid, " print ('-d%s', '%s');\n", fmt, fn);
fprintf (fid, " t_print = toc ();\n");
fprintf (fid, " fprintf ('[%%f %%f] done\\n',t_plot, t_print);\n");
fprintf (fid, " end\n");
## Temporary fix for cruft accumulating in figure window.
fprintf (fid, " close ('all');\n");
fprintf (fid, " else\n");
fprintf (fid, " fprintf ('File ""%s"" already exists.\\n');\n", fn);
fprintf (fid, " end\n");
fprintf (fid, "catch\n");
fprintf (fid, " fprintf ('ERROR in %s: %%s\\n', lasterr ());\n", base_fcn);
fprintf (fid, " err_fid = fopen ('%s.err', 'w');\n", base_fcn);
fprintf (fid, " fprintf (err_fid, '%%s', lasterr ());\n");
fprintf (fid, " fclose (err_fid);\n");
fprintf (fid, "end\n");
endfor
endfor
fprintf (fid, "\nclose all\n");
endfunction
function retval = get_demos (fcn)
[code, idx] = test (fcn, "grabdemo");
num_demos = length (idx) - 1;
retval = cell (1, num_demos);
## Now split the demos into a cell array
for k = 1:num_demos
retval{k} = oct2mat (code(idx(k):idx(k+1)-1));
endfor
endfunction
function code = oct2mat (code)
## Simple hacks to make things Matlab compatible
code = strrep (code, "%!", "%%"); # system operator !
code = strrep (code, "!", "~"); # logical not operator
## Simply replacing double quotes with single quotes
## causes problems with strings like 'hello "world"' or transpose.
## Test input for double quote replacement:
## title ("bar");
## a'
## foo 'bar' "baz"
## image (repmat ((1:64)', 1, 64));
## fprintf ('File "brighten_01.png" already exists.\n');
## title ({'x^2 + y^2'; 'plotted over circular disk with "circ"'});
## annotation ('textbox', [0.1 0 0.8 1], 'string', ...
## '"headstyle" property:', ...
code = regexprep (code, "[(,;\n][ ]*'[^']*'(*SKIP)(*F)|\"", "'",
"lineanchors", "dotexceptnewline");
## replace # not inside single quotes
code = regexprep (code, "[(,;\n][ ]*'[^']*'(*SKIP)(*F)|#", "%");
## Shorten all long forms of endXXX to 'end'
endkeywords = {"endfor", "endfunction", "endif", "endwhile", "end_try_catch"};
for k = 1:numel (endkeywords)
code = strrep (code, endkeywords{k}, "end");
endfor
## Comment keywords unknown to Matlab
commentkeywords = {"unwind_protect", "end_unwind_protect"};
for k = 1:numel (commentkeywords)
code = strrep (code, commentkeywords{k}, ["%" commentkeywords{k}]);
endfor
## Fix in-place operators
code = regexprep (code, '(\S+)(\s*)(.?[+-/*])=', '$1 = $1 $3');
## Fix x = y = z = XXX assignment
code = regexprep (code, '^\s*([^=\s]+)\s*=\s*([^=\s]+)\s*=\s*([^=\s]+)\s*=\s*([^=\n]+)$', ...
"$1 = $4\n$2 = $4\n$3 = $4", "lineanchors");
## Fix x = y = XXX assignment
code = regexprep (code, '^\s*([^=\s]+)\s*=\s*([^=\s]+)\s*=\s*([^=\n]+)$', ...
"$1 = $3\n$2 = $3", "lineanchors");
endfunction
function dump_helper_fcns (fid)
fprintf (fid, "\n%s\n", repmat ("%", [1, 60]));
fdisp (fid, "% Helper functions");
fprintf (fid, "%s\n", repmat ("%", [1, 60]));
## Add sombrero function
fdisp (fid, [
"function [x, y, z] = sombrero (n) "
" "
" if (nargin == 0) "
" n = 41; "
" end "
" "
" [xx, yy] = meshgrid (linspace (-8, 8, n)); "
" r = sqrt (xx.^2 + yy.^2) + eps; % eps prevents div/0 errors "
" zz = sin (r) ./ r; "
" "
" if (nargout == 0) "
" surf (xx, yy, zz); "
" elseif (nargout == 1) "
" x = zz; "
" else "
" x = xx; "
" y = yy; "
" z = zz; "
" end "
" "
"end "
]); # End of sombrero dump
fprintf (fid, "\n");
## Add rgbplot function
fdisp (fid, [
"function h = rgbplot (cmap, style) "
" "
" if (nargin == 1) "
" style = 'profile'; "
" end "
" "
" idx = 1:rows (cmap); "
" switch (lower (style)) "
" case 'profile' "
" htmp = plot (idx, cmap(:,1), 'r', ... "
" idx, cmap(:,2), 'g', ... "
" idx, cmap(:,3), 'b'); "
" set (gca (), 'ytick', 0:0.1:1); "
" set (gca (), 'xlim', [0 rows(cmap)]); "
" case 'composite' "
" htmp = image (idx); "
" set (gca, 'ytick', []); "
" colormap (cmap); "
" end "
" xlabel ('color index'); "
" "
" if (nargout > 0) "
" h = htmp; "
" end "
" "
"end "
]); # End of rgbplot dump
fprintf (fid, "\n");
fprintf (fid, "\n%s\n", repmat ("%", [1, 60]));
endfunction
|