?? rep_utils.m
字號:
function aout = rep_utils(action,fmt,fid)%REP_UTILS Utilities for print reports and report elements.% % aout = rep_utils(action,fmt,[fid])% % Input and output arguments ([]'s are optional): % action (string) action identifier% (cell array) {action,par1,par2,...}% the action identifier, followed by action % parameters% [fmt] (string) format of output, 'txt' by default% [fid] (scalar) output file id, by default NaN in which% case output is not written, only returned% in aout% % aout (varies) output of the action%% Here are the actions and their arguments: % 'printlines' par1 (cellstr) print par1, each cell on a new line% 'header' par1 (string) print document header using par1 as title% 'footer' print document footer% 'compile' par1 (string) compile the named document (only 'ps' and 'pdf')% 'inserttable' par1 (struct) print given table% par2 (scalar) print lines between rows if par2=1% par3 (scalar) use longtable format (only 'ps' and 'pdf')% 'printfigure' par1 (string) print current figure to file, par1 = filename% par2 (scalar) used resolution (150 dpi by default)% par3 (scalar) if par3=1, insert figure in minipage% 'insertfigure' par1 (string) insert figure to report, par1 = filename of figure% par2 (vector) size 2 x 1, size of figure relative to page size % NaN = automatic scaling% par3 (scalar) if par3=1, insert figure in minipage (only 'ps' and 'pdf')% 'insertbreak' insert paragraph break into report %% See also REP_STATS.% Contributed to SOM Toolbox 2.0, January 2nd, 2002 by Juha Vesanto% Copyright (c) by Juha Vesanto% http://www.cis.hut.fi/projects/somtoolbox/% Version 2.0beta juuso 020102%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% input argumentspars = {''}; if iscell(action), if length(action)>1, pars = action(2:end); end action = action{1}; endif nargin<2 | isempty(fmt), fmt = 'txt'; endglobal REPORT_OUTPUT_FMTREPORT_OUTPUT_FMT = fmt;if nargin<3 | isempty(fid), fid = NaN; end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% actionaout = []; printable = 0; switch action,case 'printlines', aout = pars{1}; case 'header', switch fmt, case {'ps','pdf'}, aout = tex_startdocument(pars{1}); case 'html', aout = html_startpage(pars{1}); case 'txt', aout = cell(0); end printable = 1; case 'footer', switch fmt, case {'ps','pdf'}, aout = tex_enddocument; case 'html', aout = html_endpage; case 'txt', aout = cell(0); end printable = 1; case 'compile', aout = compiledocument(pars{1});case 'inserttable', aout = inserttable(pars{:}); printable = 1; case 'printfigure', printfigure(pars{:});case 'insertfigure', aout = insertfigure(pars{:}); printable = 1;case 'insertbreak', aout = insertbreak; printable = 1; case 'joinstr', aout = joinstr(pars{:}); printable = 1; case 'rulestr', aout = rulestr(pars{:}); printable = 1; case 'c_and_p_str', aout = c_and_p_str(pars{:}); printable = 1; case 'p_str', aout = p_str(pars{:}); printable = 1; end % if output file is given, print linesif ~isnan(fid) & printable, if ~iscell(aout), aout = {aout}; end for i = 1:length(aout), fprintf(fid,'%s\n',fmtline(aout{i})); endendreturn;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% subfunctions%% simple formatter stringsfunction s = joinstr(cs, sep1, sep2) if nargin==1, sep1 = ', '; sep2 = ' and '; end if nargin<3, sep2 = sep1; end if isempty(cs), s = ''; elseif strcmp(sep1,'\n'), if size(cs,1)==1, cs = cs'; end s = char(cs); else s = cs{1}; for i=2:length(cs)-1, s = [s sep1 cs{i}]; end if length(cs)>1, s = [s sep2 cs{end}]; end end return; function str = c_and_p_str(n,m) % return a string of form # (%), e.g. '23 (12%)' if n==m, p = '100'; elseif n==0, p = '0'; else p = sprintf('%.2g',100*n/m); end str = sprintf('%d (%s%%)',round(n),p); return; function str = p_str(p) % return a string of form %, e.g. '12%' if round(p*100)>=100, p = sprintf('%3g',100*p); elseif abs(p)<eps, p = '0'; else p = sprintf('%.2g',100*p); end str = sprintf('%s%%',p); return;function cs = rulestr(sR,cnames) global REPORT_OUTPUT_FMT switch REPORT_OUTPUT_FMT case {'ps','pdf'}, [leq,geq,infi,m,less,in] = deal('\leq','\geq','\inf','$','<','\in'); case 'html', [leq,geq,infi,m,less,in] = deal('<=','>=','Inf',' ','<',' '); case 'txt', [leq,geq,infi,m,less,in] = deal('<=','>=','inf',' ','<',''); end nr = length(sR); cs = cell(nr,1); fmt = '%.2g'; if nargin<2, cnames = {sR.name}; end if isempty(cnames), cnames = cell(nr,1); cnames(:) = {''}; end for i=1:nr, low = sR(i).low; high = sR(i).high; switch isfinite(low) + 2*isfinite(high), case 0, cs{i} = [cnames{i} ' ' 'any']; case 1, cs{i} = [cnames{i} ' ' m geq sprintf(fmt,low) m]; case 2, cs{i} = [cnames{i} ' ' m less sprintf(fmt,high) m]; case 3, cs{i} = [cnames{i} ' ' m in '[' sprintf(fmt,low) ',' sprintf(fmt,high) ']' m]; end end return; %% print figurefunction imgfmt = fmt2imgfmt global REPORT_OUTPUT_FMT switch REPORT_OUTPUT_FMT, case 'ps', imgfmt = 'ps'; case 'pdf', imgfmt = 'pdf'; case 'html', imgfmt = 'png'; case 'txt', imgfmt = ''; end return; function printfigure(fname,resolution) if nargin<2, resolution = 150; end fnameps = [fname '.ps']; switch fmt2imgfmt, case 'ps', print('-dpsc2',fnameps); case 'pdf', print('-dpsc2',fnameps); eval(sprintf('!ps2pdf %s',fnameps)); case 'gif', print('-dpsc2',fnameps); cmd = 'pstogif'; opt = sprintf('-depth 1 -density %d',resolution); unix(sprintf('%s %s -out %s %s',cmd,opt,[fname '.gif'],fnameps)); case 'png', opt = sprintf('-r%d',resolution); print('-dpng',opt,[fname '.png']); end return; %% headers and footers, and compilationfunction cs = tex_startdocument(title) % tex document headers global REPORT_OUTPUT_FMT cs = cell(0); cs{end+1} = '\documentclass[10pt,a4paper]{article}'; cs{end+1} = '\usepackage[dvips]{epsfig,graphicx,color}'; cs{end+1} = '\usepackage{float,graphics,subfigure}'; cs{end+1} = '\usepackage{multirow,rotating,portland,lscape,longtable,pifont}'; cs{end+1} = '\usepackage[T1]{fontenc}'; if strcmp(REPORT_OUTPUT_FMT,'pdf'), cs{end+1} = '\usepackage{pslatex}'; end cs{end+1} = '\usepackage[english]{babel}'; cs{end+1} = '\oddsidemargin 0 mm'; cs{end+1} = '\evensidemargin 0 mm'; cs{end+1} = '\textwidth 17 cm'; cs{end+1} = '\topmargin 0 mm'; cs{end+1} = '\textheight 21 cm'; cs{end+1} = '\voffset 0 mm'; cs{end+1} = '\begin{document}'; cs{end+1} = ['\title{' title '}']; cs{end+1} = '\maketitle'; %cs{end+1} = '\tableofcontents'; %cs{end+1} = '\clearpage'; return;function cs = tex_enddocument cs = cell(0); cs{end+1} = '\end{document}'; return; function cs = html_startpage(title) % print HTML document headers cs = cell(0); cs{end+1} = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'; cs{end+1} = '<HTML>'; cs{end+1} = '<HEAD>'; cs{end+1} = sprintf(' <TITLE>%s</TITLE>',title); cs{end+1} = '</HEAD>'; cs{end+1} = '<BODY bgcolor=white vlink="#000033" link="#0000ff" text="#000000">'; if ~isempty(title), cs{end+1} = sprintf('<H1>%s</H1>',title); end return;function cs = html_endpage % print HTML document footers cs = cell(0); cs{end+1} = '<P><HR>'; cs{end+1} = '</BODY>'; cs{end+1} = '</HTML>'; return;function files = compiledocument(filename) global REPORT_OUTPUT_FMT switch REPORT_OUTPUT_FMT, case 'pdf', eval(sprintf('!pdflatex --interaction batchmode %s.tex',filename)); eval(sprintf('!pdflatex --interaction batchmode %s.tex',filename)); %eval(sprintf('!acroread %s.pdf &',filename)); files = {[filename '.aux'],[filename '.log'],[filename '.out'],[filename '.pdf']}; case 'ps', eval(sprintf('!latex --interaction batchmode %s.tex',filename)); eval(sprintf('!latex --interaction batchmode %s.tex',filename)); eval(sprintf('!dvips %s.dvi',filename)); eval(sprintf('!ps2pdf %s.ps',filename)); %eval(sprintf('!ghostview %s.ps &',filename)); files = {[filename '.aux'],[filename '.log'],[filename '.out'],[filename '.dvi'],[filename '.pdf']}; case 'html', case 'txt', end return; function vstr = defaultformat(val) global REPORT_OUTPUT_FMT if ischar(val), vstr = val; elseif iscellstr(val), vstr = char(val); elseif isempty(val), vstr = ''; elseif isnumeric(val), if val==round(val), fmt = '%d'; else fmt = '%.3g'; end if abs(val)<=eps, vstr = '0'; else vstr = sprintf(fmt,val); end elseif isstruct(val) & isfield(val,'values') & isfield(val,'headers'), % a table vstr = joinstr(inserttable(val,0),'\n'); if any(strcmp(REPORT_OUTPUT_FMT,{'ps','pdf'})), vstr= inserttominipage(vstr); end else vstr = ''; fprintf(1,'defaultformat unable to handle input\n'); whos val end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -