function dump_demos (directory, output) ## Get path if (nargin == 0) dirs = strsplit (path (), pathsep ()); elseif (nargin == 2) if (is_absolute_filename (directory)) dirs = {directory}; else fullname = find_dir_in_path (directory); if (! isempty (fullname)) dirs = {fullname}; else error ("print_demos: expecting argument to be a directory name"); endif endif else print_usage (); endif ## Create script beginning (close figures, etc.) fid = fopen (output, "w"); fprintf (fid, "visible = get (0, 'defaultfigurevisible');\n"); fprintf (fid, "set (0, 'defaultfigurevisible', 'off');\n"); fprintf (fid, "\n"); ## Run and print the demos in each directory for i = 1:numel (dirs) d = dirs{i}; dump_all_demos (d, fid); endfor ## Create script ending fprintf (fid, "set (0, 'defaultfigurevisible', visible);\n"); fclose (fid); endfunction function dump_all_demos (directory, fid) dirinfo = dir (fullfile (directory, "*.m")); flist = {dirinfo.name}; for i = 1:numel (flist) fun = flist{i}; fun (end-1:end) = []; # remove .m demos = get_demos (fun); for d = 1:numel (demos) fprintf (fid, "\ntry\n"); fprintf (fid, " %s\n\n", demos {d}); fprintf (fid, " fignum = 1;\n"); fprintf (fid, " while (~isempty (get (0, 'currentfigure')))\n"); fprintf (fid, " fig = gcf ();\n"); fprintf (fid, " figure (fig);\n"); fprintf (fid, " name = sprintf ('%s_%d_%%d.png', fignum);\n", fun, d); fprintf (fid, " print (name, '-dpng');\n"); fprintf (fid, " close (fig);\n"); fprintf (fid, " fignum = fignum + 1;\n"); fprintf (fid, " end\n"); fprintf (fid, "catch\n"); fprintf (fid, " fprintf ('%s: %%s\\n', lasterr ());\n", fun); fprintf (fid, "end\n\n"); endfor endfor endfunction function retval = get_demos (fun) [code, idx] = test (fun, "grabdemo"); num_demos = length (idx) - 1; retval = cell (1, num_demos); ## Simple hacks to make things Matlab compatible code = strrep (code, "\"", "'"); code = strrep (code, "#", "%"); endkeywords = {"endfor", "endif", "endwhile", "end_try_catch"}; for k = 1:numel (endkeywords) in = endkeywords {k}; out = in; out (4:end) = " "; code = strrep (code, in, out); endfor ## Now split the demos into a cell array for k = 1:num_demos retval {k} = code (idx (k):idx (k+1)-1); endfor endfunction