annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/rep_utils.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function aout = rep_utils(action,fmt,fid)
Daniel@0 2
Daniel@0 3 %REP_UTILS Utilities for print reports and report elements.
Daniel@0 4 %
Daniel@0 5 % aout = rep_utils(action,fmt,[fid])
Daniel@0 6 %
Daniel@0 7 % Input and output arguments ([]'s are optional):
Daniel@0 8 % action (string) action identifier
Daniel@0 9 % (cell array) {action,par1,par2,...}
Daniel@0 10 % the action identifier, followed by action
Daniel@0 11 % parameters
Daniel@0 12 % [fmt] (string) format of output, 'txt' by default
Daniel@0 13 % [fid] (scalar) output file id, by default NaN in which
Daniel@0 14 % case output is not written, only returned
Daniel@0 15 % in aout
Daniel@0 16 %
Daniel@0 17 % aout (varies) output of the action
Daniel@0 18 %
Daniel@0 19 % Here are the actions and their arguments:
Daniel@0 20 % 'printlines' par1 (cellstr) print par1, each cell on a new line
Daniel@0 21 % 'header' par1 (string) print document header using par1 as title
Daniel@0 22 % 'footer' print document footer
Daniel@0 23 % 'compile' par1 (string) compile the named document (only 'ps' and 'pdf')
Daniel@0 24 % 'inserttable' par1 (struct) print given table
Daniel@0 25 % par2 (scalar) print lines between rows if par2=1
Daniel@0 26 % par3 (scalar) use longtable format (only 'ps' and 'pdf')
Daniel@0 27 % 'printfigure' par1 (string) print current figure to file, par1 = filename
Daniel@0 28 % par2 (scalar) used resolution (150 dpi by default)
Daniel@0 29 % par3 (scalar) if par3=1, insert figure in minipage
Daniel@0 30 % 'insertfigure' par1 (string) insert figure to report, par1 = filename of figure
Daniel@0 31 % par2 (vector) size 2 x 1, size of figure relative to page size
Daniel@0 32 % NaN = automatic scaling
Daniel@0 33 % par3 (scalar) if par3=1, insert figure in minipage (only 'ps' and 'pdf')
Daniel@0 34 % 'insertbreak' insert paragraph break into report
Daniel@0 35 %
Daniel@0 36 % See also REP_STATS.
Daniel@0 37
Daniel@0 38 % Contributed to SOM Toolbox 2.0, January 2nd, 2002 by Juha Vesanto
Daniel@0 39 % Copyright (c) by Juha Vesanto
Daniel@0 40 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 41
Daniel@0 42 % Version 2.0beta juuso 020102
Daniel@0 43
Daniel@0 44 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 45 %% input arguments
Daniel@0 46
Daniel@0 47 pars = {''};
Daniel@0 48 if iscell(action),
Daniel@0 49 if length(action)>1, pars = action(2:end); end
Daniel@0 50 action = action{1};
Daniel@0 51 end
Daniel@0 52
Daniel@0 53 if nargin<2 | isempty(fmt), fmt = 'txt'; end
Daniel@0 54 global REPORT_OUTPUT_FMT
Daniel@0 55 REPORT_OUTPUT_FMT = fmt;
Daniel@0 56
Daniel@0 57 if nargin<3 | isempty(fid), fid = NaN; end
Daniel@0 58
Daniel@0 59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 60 %% action
Daniel@0 61
Daniel@0 62 aout = [];
Daniel@0 63 printable = 0;
Daniel@0 64
Daniel@0 65 switch action,
Daniel@0 66 case 'printlines',
Daniel@0 67 aout = pars{1};
Daniel@0 68 case 'header',
Daniel@0 69 switch fmt,
Daniel@0 70 case {'ps','pdf'}, aout = tex_startdocument(pars{1});
Daniel@0 71 case 'html', aout = html_startpage(pars{1});
Daniel@0 72 case 'txt', aout = cell(0);
Daniel@0 73 end
Daniel@0 74 printable = 1;
Daniel@0 75 case 'footer',
Daniel@0 76 switch fmt,
Daniel@0 77 case {'ps','pdf'}, aout = tex_enddocument;
Daniel@0 78 case 'html', aout = html_endpage;
Daniel@0 79 case 'txt', aout = cell(0);
Daniel@0 80 end
Daniel@0 81 printable = 1;
Daniel@0 82 case 'compile', aout = compiledocument(pars{1});
Daniel@0 83 case 'inserttable', aout = inserttable(pars{:}); printable = 1;
Daniel@0 84 case 'printfigure', printfigure(pars{:});
Daniel@0 85 case 'insertfigure', aout = insertfigure(pars{:}); printable = 1;
Daniel@0 86 case 'insertbreak', aout = insertbreak; printable = 1;
Daniel@0 87 case 'joinstr', aout = joinstr(pars{:}); printable = 1;
Daniel@0 88 case 'rulestr', aout = rulestr(pars{:}); printable = 1;
Daniel@0 89 case 'c_and_p_str', aout = c_and_p_str(pars{:}); printable = 1;
Daniel@0 90 case 'p_str', aout = p_str(pars{:}); printable = 1;
Daniel@0 91 end
Daniel@0 92
Daniel@0 93 % if output file is given, print lines
Daniel@0 94 if ~isnan(fid) & printable,
Daniel@0 95 if ~iscell(aout), aout = {aout}; end
Daniel@0 96 for i = 1:length(aout), fprintf(fid,'%s\n',fmtline(aout{i})); end
Daniel@0 97 end
Daniel@0 98
Daniel@0 99 return;
Daniel@0 100
Daniel@0 101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 102 %% subfunctions
Daniel@0 103
Daniel@0 104 %% simple formatter strings
Daniel@0 105
Daniel@0 106 function s = joinstr(cs, sep1, sep2)
Daniel@0 107 if nargin==1, sep1 = ', '; sep2 = ' and '; end
Daniel@0 108 if nargin<3, sep2 = sep1; end
Daniel@0 109 if isempty(cs),
Daniel@0 110 s = '';
Daniel@0 111 elseif strcmp(sep1,'\n'),
Daniel@0 112 if size(cs,1)==1, cs = cs'; end
Daniel@0 113 s = char(cs);
Daniel@0 114 else
Daniel@0 115 s = cs{1};
Daniel@0 116 for i=2:length(cs)-1, s = [s sep1 cs{i}]; end
Daniel@0 117 if length(cs)>1, s = [s sep2 cs{end}]; end
Daniel@0 118 end
Daniel@0 119 return;
Daniel@0 120
Daniel@0 121 function str = c_and_p_str(n,m)
Daniel@0 122
Daniel@0 123 % return a string of form # (%), e.g. '23 (12%)'
Daniel@0 124 if n==m, p = '100';
Daniel@0 125 elseif n==0, p = '0';
Daniel@0 126 else p = sprintf('%.2g',100*n/m);
Daniel@0 127 end
Daniel@0 128 str = sprintf('%d (%s%%)',round(n),p);
Daniel@0 129 return;
Daniel@0 130
Daniel@0 131 function str = p_str(p)
Daniel@0 132 % return a string of form %, e.g. '12%'
Daniel@0 133 if round(p*100)>=100, p = sprintf('%3g',100*p);
Daniel@0 134 elseif abs(p)<eps, p = '0';
Daniel@0 135 else p = sprintf('%.2g',100*p);
Daniel@0 136 end
Daniel@0 137 str = sprintf('%s%%',p);
Daniel@0 138 return;
Daniel@0 139
Daniel@0 140 function cs = rulestr(sR,cnames)
Daniel@0 141 global REPORT_OUTPUT_FMT
Daniel@0 142 switch REPORT_OUTPUT_FMT
Daniel@0 143 case {'ps','pdf'}, [leq,geq,infi,m,less,in] = deal('\leq','\geq','\inf','$','<','\in');
Daniel@0 144 case 'html', [leq,geq,infi,m,less,in] = deal('&lt;=','&gt;=','Inf',' ','&lt;',' ');
Daniel@0 145 case 'txt', [leq,geq,infi,m,less,in] = deal('<=','>=','inf',' ','<','');
Daniel@0 146 end
Daniel@0 147 nr = length(sR);
Daniel@0 148 cs = cell(nr,1);
Daniel@0 149 fmt = '%.2g';
Daniel@0 150 if nargin<2, cnames = {sR.name}; end
Daniel@0 151 if isempty(cnames), cnames = cell(nr,1); cnames(:) = {''}; end
Daniel@0 152 for i=1:nr,
Daniel@0 153 low = sR(i).low;
Daniel@0 154 high = sR(i).high;
Daniel@0 155 switch isfinite(low) + 2*isfinite(high),
Daniel@0 156 case 0, cs{i} = [cnames{i} ' ' 'any'];
Daniel@0 157 case 1, cs{i} = [cnames{i} ' ' m geq sprintf(fmt,low) m];
Daniel@0 158 case 2, cs{i} = [cnames{i} ' ' m less sprintf(fmt,high) m];
Daniel@0 159 case 3, cs{i} = [cnames{i} ' ' m in '[' sprintf(fmt,low) ',' sprintf(fmt,high) ']' m];
Daniel@0 160 end
Daniel@0 161 end
Daniel@0 162 return;
Daniel@0 163
Daniel@0 164 %% print figure
Daniel@0 165
Daniel@0 166 function imgfmt = fmt2imgfmt
Daniel@0 167 global REPORT_OUTPUT_FMT
Daniel@0 168 switch REPORT_OUTPUT_FMT,
Daniel@0 169 case 'ps', imgfmt = 'ps';
Daniel@0 170 case 'pdf', imgfmt = 'pdf';
Daniel@0 171 case 'html', imgfmt = 'png';
Daniel@0 172 case 'txt', imgfmt = '';
Daniel@0 173 end
Daniel@0 174 return;
Daniel@0 175
Daniel@0 176 function printfigure(fname,resolution)
Daniel@0 177 if nargin<2, resolution = 150; end
Daniel@0 178 fnameps = [fname '.ps'];
Daniel@0 179 switch fmt2imgfmt,
Daniel@0 180 case 'ps',
Daniel@0 181 print('-dpsc2',fnameps);
Daniel@0 182 case 'pdf',
Daniel@0 183 print('-dpsc2',fnameps);
Daniel@0 184 eval(sprintf('!ps2pdf %s',fnameps));
Daniel@0 185 case 'gif',
Daniel@0 186 print('-dpsc2',fnameps);
Daniel@0 187 cmd = 'pstogif';
Daniel@0 188 opt = sprintf('-depth 1 -density %d',resolution);
Daniel@0 189 unix(sprintf('%s %s -out %s %s',cmd,opt,[fname '.gif'],fnameps));
Daniel@0 190 case 'png',
Daniel@0 191 opt = sprintf('-r%d',resolution);
Daniel@0 192 print('-dpng',opt,[fname '.png']);
Daniel@0 193 end
Daniel@0 194 return;
Daniel@0 195
Daniel@0 196 %% headers and footers, and compilation
Daniel@0 197
Daniel@0 198 function cs = tex_startdocument(title)
Daniel@0 199 % tex document headers
Daniel@0 200 global REPORT_OUTPUT_FMT
Daniel@0 201 cs = cell(0);
Daniel@0 202 cs{end+1} = '\documentclass[10pt,a4paper]{article}';
Daniel@0 203 cs{end+1} = '\usepackage[dvips]{epsfig,graphicx,color}';
Daniel@0 204 cs{end+1} = '\usepackage{float,graphics,subfigure}';
Daniel@0 205 cs{end+1} = '\usepackage{multirow,rotating,portland,lscape,longtable,pifont}';
Daniel@0 206 cs{end+1} = '\usepackage[T1]{fontenc}';
Daniel@0 207 if strcmp(REPORT_OUTPUT_FMT,'pdf'), cs{end+1} = '\usepackage{pslatex}'; end
Daniel@0 208 cs{end+1} = '\usepackage[english]{babel}';
Daniel@0 209
Daniel@0 210 cs{end+1} = '\oddsidemargin 0 mm';
Daniel@0 211 cs{end+1} = '\evensidemargin 0 mm';
Daniel@0 212 cs{end+1} = '\textwidth 17 cm';
Daniel@0 213 cs{end+1} = '\topmargin 0 mm';
Daniel@0 214 cs{end+1} = '\textheight 21 cm';
Daniel@0 215 cs{end+1} = '\voffset 0 mm';
Daniel@0 216
Daniel@0 217 cs{end+1} = '\begin{document}';
Daniel@0 218 cs{end+1} = ['\title{' title '}'];
Daniel@0 219 cs{end+1} = '\maketitle';
Daniel@0 220 %cs{end+1} = '\tableofcontents';
Daniel@0 221 %cs{end+1} = '\clearpage';
Daniel@0 222 return;
Daniel@0 223
Daniel@0 224 function cs = tex_enddocument
Daniel@0 225 cs = cell(0);
Daniel@0 226 cs{end+1} = '\end{document}';
Daniel@0 227 return;
Daniel@0 228
Daniel@0 229 function cs = html_startpage(title)
Daniel@0 230 % print HTML document headers
Daniel@0 231 cs = cell(0);
Daniel@0 232 cs{end+1} = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">';
Daniel@0 233 cs{end+1} = '<HTML>';
Daniel@0 234 cs{end+1} = '<HEAD>';
Daniel@0 235 cs{end+1} = sprintf(' <TITLE>%s</TITLE>',title);
Daniel@0 236 cs{end+1} = '</HEAD>';
Daniel@0 237 cs{end+1} = '<BODY bgcolor=white vlink="#000033" link="#0000ff" text="#000000">';
Daniel@0 238 if ~isempty(title), cs{end+1} = sprintf('<H1>%s</H1>',title); end
Daniel@0 239 return;
Daniel@0 240
Daniel@0 241 function cs = html_endpage
Daniel@0 242 % print HTML document footers
Daniel@0 243 cs = cell(0);
Daniel@0 244 cs{end+1} = '<P><HR>';
Daniel@0 245 cs{end+1} = '</BODY>';
Daniel@0 246 cs{end+1} = '</HTML>';
Daniel@0 247 return;
Daniel@0 248
Daniel@0 249 function files = compiledocument(filename)
Daniel@0 250 global REPORT_OUTPUT_FMT
Daniel@0 251 switch REPORT_OUTPUT_FMT,
Daniel@0 252 case 'pdf',
Daniel@0 253 eval(sprintf('!pdflatex --interaction batchmode %s.tex',filename));
Daniel@0 254 eval(sprintf('!pdflatex --interaction batchmode %s.tex',filename));
Daniel@0 255 %eval(sprintf('!acroread %s.pdf &',filename));
Daniel@0 256 files = {[filename '.aux'],[filename '.log'],[filename '.out'],[filename '.pdf']};
Daniel@0 257 case 'ps',
Daniel@0 258 eval(sprintf('!latex --interaction batchmode %s.tex',filename));
Daniel@0 259 eval(sprintf('!latex --interaction batchmode %s.tex',filename));
Daniel@0 260 eval(sprintf('!dvips %s.dvi',filename));
Daniel@0 261 eval(sprintf('!ps2pdf %s.ps',filename));
Daniel@0 262 %eval(sprintf('!ghostview %s.ps &',filename));
Daniel@0 263 files = {[filename '.aux'],[filename '.log'],[filename '.out'],[filename '.dvi'],[filename '.pdf']};
Daniel@0 264 case 'html',
Daniel@0 265 case 'txt',
Daniel@0 266 end
Daniel@0 267 return;
Daniel@0 268
Daniel@0 269
Daniel@0 270 function vstr = defaultformat(val)
Daniel@0 271 global REPORT_OUTPUT_FMT
Daniel@0 272 if ischar(val), vstr = val;
Daniel@0 273 elseif iscellstr(val), vstr = char(val);
Daniel@0 274 elseif isempty(val), vstr = '';
Daniel@0 275 elseif isnumeric(val),
Daniel@0 276 if val==round(val), fmt = '%d'; else fmt = '%.3g'; end
Daniel@0 277 if abs(val)<=eps, vstr = '0'; else vstr = sprintf(fmt,val); end
Daniel@0 278 elseif isstruct(val) & isfield(val,'values') & isfield(val,'headers'),
Daniel@0 279 % a table
Daniel@0 280 vstr = joinstr(inserttable(val,0),'\n');
Daniel@0 281 if any(strcmp(REPORT_OUTPUT_FMT,{'ps','pdf'})),
Daniel@0 282 vstr= inserttominipage(vstr);
Daniel@0 283 end
Daniel@0 284 else
Daniel@0 285 vstr = ''; fprintf(1,'defaultformat unable to handle input\n');
Daniel@0 286 whos val
Daniel@0 287 end
Daniel@0 288 return;
Daniel@0 289
Daniel@0 290 %% report elements (list, table, image, link)
Daniel@0 291
Daniel@0 292 function str = fmtline(str)
Daniel@0 293 % replace some formatting elements depeding on output format
Daniel@0 294 global REPORT_OUTPUT_FMT
Daniel@0 295 if isempty(str), str = ''; return; end
Daniel@0 296 switch REPORT_OUTPUT_FMT,
Daniel@0 297 case {'ps','pdf'},
Daniel@0 298 str = strrep(str,'<B>', '{\bf ');
Daniel@0 299 str = strrep(str,'<I>', '{\em ');
Daniel@0 300 str = strrep(str,'<TT>', '{\tt ');
Daniel@0 301 str = strrep(str,'</B>', '}');
Daniel@0 302 str = strrep(str,'</I>', '}');
Daniel@0 303 str = strrep(str,'</TT>','}');
Daniel@0 304 str = strrep(str,'#','\#');
Daniel@0 305 str = strrep(str,'%','\%');
Daniel@0 306 case 'html', % nil
Daniel@0 307 case 'txt',
Daniel@0 308 str = strrep(str,'<B>', '*');
Daniel@0 309 str = strrep(str,'<I>', '*');
Daniel@0 310 str = strrep(str,'<TT>', '');
Daniel@0 311 str = strrep(str,'</B>', '*');
Daniel@0 312 str = strrep(str,'</I>', '*');
Daniel@0 313 str = strrep(str,'</TT>','');
Daniel@0 314 end
Daniel@0 315 return;
Daniel@0 316
Daniel@0 317 function cs = insertbreak
Daniel@0 318 global REPORT_OUTPUT_FMT
Daniel@0 319 cs = cell(0);
Daniel@0 320 switch REPORT_OUTPUT_FMT
Daniel@0 321 case {'ps','pdf'}, cs{end+1} = '';
Daniel@0 322 case 'html', cs{end+1} = '<P>';
Daniel@0 323 case 'txt', cs{end+1} = '';
Daniel@0 324 end
Daniel@0 325 return;
Daniel@0 326
Daniel@0 327 function insertlist(list,enum)
Daniel@0 328 % make list
Daniel@0 329 global REPORT_OUTPUT_FMT
Daniel@0 330 if nargin<2, enum = 0; end
Daniel@0 331 cs = cell(0);
Daniel@0 332 switch REPORT_OUTPUT_FMT
Daniel@0 333 case {'ps','pdf'},
Daniel@0 334 if enum, tag = 'enumerate'; else tag = 'itemize'; end
Daniel@0 335 starttag = ['\begin{' tag '}'];
Daniel@0 336 listtag = '\item ';
Daniel@0 337 endtag = ['\end{' tag '}'];
Daniel@0 338 case 'html',
Daniel@0 339 if enum, tag = 'OL'; else tag = 'UL'; end
Daniel@0 340 starttag = ['<' tag '>'];
Daniel@0 341 listtag = '<LI>';
Daniel@0 342 endtag = ['</' tag '>'];
Daniel@0 343 case 'txt',
Daniel@0 344 starttag = '';
Daniel@0 345 listtag = '- ';
Daniel@0 346 endtag = '';
Daniel@0 347 end
Daniel@0 348 cs{end+1} = starttag;
Daniel@0 349 for i=1:length(list), cs{end+1} = sprintf('%s %s',listtag,list{i}); end
Daniel@0 350 cs{end+1} = endtag;
Daniel@0 351 return;
Daniel@0 352
Daniel@0 353 function csout = tablerow(cs,emp,span)
Daniel@0 354 % construct one table row
Daniel@0 355 global REPORT_OUTPUT_FMT
Daniel@0 356 if nargin<2 | isempty(emp), emp = 'none'; end
Daniel@0 357 if nargin<3 | isempty(span), span = ones(length(cs),2); end
Daniel@0 358 rowspan = span(:,1); colspan = span(:,2);
Daniel@0 359 switch emp,
Daniel@0 360 case 'bold', emp1 = '<B>'; emp2 = '</B>';
Daniel@0 361 case 'italic', emp1 = '<I>'; emp2 = '</I>';
Daniel@0 362 case 'fixed', emp1 = '<TT>'; emp2 = '</TT>';
Daniel@0 363 case 'none', emp1 = ''; emp2 = '';
Daniel@0 364 case 'header', emp1 = ''; emp2 = ''; tag = 'TH';
Daniel@0 365 end
Daniel@0 366 csout = cell(0);
Daniel@0 367 switch REPORT_OUTPUT_FMT,
Daniel@0 368 case {'pdf','ps'},
Daniel@0 369 %switch emp,
Daniel@0 370 % case 'bold', emp1 = '{\bf '; emp2 = '}';
Daniel@0 371 % case 'italic', emp1 = '{\em '; emp2 = '}';
Daniel@0 372 % case 'fixed', emp1 = '{\tt '; emp2 = '}';
Daniel@0 373 % case 'none', emp1 = ''; emp2 = '';
Daniel@0 374 %end
Daniel@0 375 s0 = '';
Daniel@0 376 for i=1:length(cs),
Daniel@0 377 if rowspan(i) & colspan(i),
Daniel@0 378 sp1 = ''; sp2 = '';
Daniel@0 379 if colspan(i)>1, sp1 = [sp1 ' \multicolumn{' num2str(colspan(i)) '}{|c|}{']; sp2 = [sp2 '}']; end
Daniel@0 380 if rowspan(i)>1, sp1 = [sp1 ' \multirow{' num2str(rowspan(i)) '}{2cm}{']; sp2 = [sp2 '}']; end
Daniel@0 381 s = s0;
Daniel@0 382 content = cellstr(defaultformat(cs{i}));
Daniel@0 383 csout{end+1} = [s sp1 emp1 content{1}];
Daniel@0 384 for j=2:length(content), csout{end+1} = content{j}; end
Daniel@0 385 csout{end} = [csout{end} emp2 sp2];
Daniel@0 386 s0 = ' & ';
Daniel@0 387 end
Daniel@0 388 end
Daniel@0 389 csout{end} = [csout{end} ' \\'];
Daniel@0 390 case 'html',
Daniel@0 391 tag = 'TD';
Daniel@0 392 csout{end+1} = '<TR>';
Daniel@0 393 for i=1:length(cs),
Daniel@0 394 if rowspan(i) & colspan(i),
Daniel@0 395 sp = '';
Daniel@0 396 if rowspan(i)>1, sp = [sp ' ROWSPAN=' num2str(rowspan(i))]; end
Daniel@0 397 if colspan(i)>1, sp = [sp ' COLSPAN=' num2str(colspan(i))]; end
Daniel@0 398 s = sprintf('<%s%s>%s',tag,sp,emp1);
Daniel@0 399 content = cellstr(defaultformat(cs{i}));
Daniel@0 400 csout{end+1} = [s content{1}];
Daniel@0 401 for j=2:length(content), csout{end+1} = content{j}; end
Daniel@0 402 csout{end} = [csout{end} emp2 '</' tag '>'];
Daniel@0 403 end
Daniel@0 404 end
Daniel@0 405 csout{end+1} = '</TR>';
Daniel@0 406 case 'txt',
Daniel@0 407 for i=1:length(cs), csout{end+1} = defaultformat(cs{i}); end
Daniel@0 408 end
Daniel@0 409 return;
Daniel@0 410
Daniel@0 411 function cs = inserttable(sTable,rowlines,long)
Daniel@0 412 % put table contents to cellstr
Daniel@0 413 global REPORT_OUTPUT_FMT
Daniel@0 414 if nargin<2, rowlines = 1; end
Daniel@0 415 if nargin<3, long = 0; end
Daniel@0 416 [rows cols] = size(sTable.values);
Daniel@0 417 cs = cell(0);
Daniel@0 418 if isempty(sTable.colfmt), cf = 'c'; sTable.colfmt = cf(ones(1,cols)); end
Daniel@0 419 if isempty(sTable.span), sTable.span = ones([rows cols 2]); end
Daniel@0 420 switch REPORT_OUTPUT_FMT
Daniel@0 421 case {'ps','pdf','tex','latex'}
Daniel@0 422 li1 = ' \hline';
Daniel@0 423 if rowlines>0, li2 = li1; li3 = li1;
Daniel@0 424 elseif rowlines==0, li2 = ''; li3 = li1;
Daniel@0 425 else li1 = ''; li2 = ''; li3 = '';
Daniel@0 426 end
Daniel@0 427 if long, tbl = 'longtable'; else tbl = 'tabular'; end
Daniel@0 428 cs{end+1} = ['\begin{' tbl '}{' sTable.colfmt '}' li1];
Daniel@0 429 if ~isempty(sTable.headers),
Daniel@0 430 row = tablerow(sTable.headers,'bold');
Daniel@0 431 for i=1:length(row), cs{end+1} = row{i}; end
Daniel@0 432 cs{end} = [cs{end} li1 li2];
Daniel@0 433 end
Daniel@0 434 for i=1:rows,
Daniel@0 435 row = tablerow(sTable.values(i,:),'',squeeze(sTable.span(i,:,:)));
Daniel@0 436 for i=1:length(row), cs{end+1} = row{i}; end
Daniel@0 437 cs{end} = [cs{end} li2];
Daniel@0 438 end
Daniel@0 439 if ~rowlines, cs{end} = [cs{end} li3]; end
Daniel@0 440 cs{end+1} = ['\end{' tbl '}'];
Daniel@0 441 case 'html'
Daniel@0 442 cs{end+1} = ['<TABLE BORDER=' num2str(rowlines>0) '>'];
Daniel@0 443 if ~isempty(sTable.headers),
Daniel@0 444 row = tablerow(sTable.headers,'header');
Daniel@0 445 for i=1:length(row), cs{end+1} = row{i}; end
Daniel@0 446 end
Daniel@0 447 for i=1:rows,
Daniel@0 448 row = tablerow(sTable.values(i,:),'',squeeze(sTable.span(i,:,:)));
Daniel@0 449 for i=1:length(row), cs{end+1} = row{i}; end
Daniel@0 450 end
Daniel@0 451 cs{end+1} = '</TABLE>';
Daniel@0 452 case 'txt'
Daniel@0 453 cT = [sTable.headers(:)'; sTable.values];
Daniel@0 454 A = cell2char(cT);
Daniel@0 455 for i=1:size(A,1), cs{end+1} = A(i,:); end
Daniel@0 456 end
Daniel@0 457 return;
Daniel@0 458
Daniel@0 459 function A = cell2char(T)
Daniel@0 460
Daniel@0 461 [nrow,ncol] = size(T);
Daniel@0 462 rowsep = 0;
Daniel@0 463 colsep = 1;
Daniel@0 464
Daniel@0 465 % change to strings
Daniel@0 466 for i=1:nrow,
Daniel@0 467 for j=1:ncol,
Daniel@0 468 t = T{i,j};
Daniel@0 469 if ischar(t), % ok
Daniel@0 470 elseif isempty(t), T{i,j} = '';
Daniel@0 471 elseif isstruct(t), % ??
Daniel@0 472 elseif iscell(t), T{i,j} = cell2char(t);
Daniel@0 473 elseif isnumeric(t), T{i,j} = num2str(t,3);
Daniel@0 474 end
Daniel@0 475 end
Daniel@0 476 end
Daniel@0 477
Daniel@0 478 % widths of columns and heights of rows
Daniel@0 479 HW = ones(nrow,ncol,2);
Daniel@0 480 for i=1:nrow, for j=1:ncol, HW(i,j,:) = size(T{i,j}); end, end
Daniel@0 481 colw = max(HW(:,:,2),[],1);
Daniel@0 482 rowh = max(HW(:,:,1),[],2);
Daniel@0 483
Daniel@0 484 % the table itself
Daniel@0 485 A = char(32*ones(sum(rowh)+rowsep*(nrow-1),sum(colw)+colsep*(ncol-1)));
Daniel@0 486 for i=1:nrow,
Daniel@0 487 for j=1:ncol,
Daniel@0 488 i0 = (i-1)*rowsep+sum(rowh(1:i-1));
Daniel@0 489 j0 = (j-1)*colsep+sum(colw(1:j-1));
Daniel@0 490 S = char(32*ones(rowh(i),colw(j)));
Daniel@0 491 si = size(T{i,j}); S(1:si(1),1:si(2)) = T{i,j};
Daniel@0 492 A(i0+[1:rowh(i)],j0+[1:colw(j)]) = S;
Daniel@0 493 end
Daniel@0 494 end
Daniel@0 495 return;
Daniel@0 496
Daniel@0 497
Daniel@0 498 function s = inserttominipage(s,width)
Daniel@0 499 if nargin<2 | isempty(width) | isnan(width), width = 1; end
Daniel@0 500 width = ['{' num2str(width) '\columnwidth}'];
Daniel@0 501 mp1 = '\begin{minipage}[t]'; mp2 = '\end{minipage}';
Daniel@0 502 if size(s,1)==1, s = [mp1 width s mp2];
Daniel@0 503 else s = char({[mp1 width]; s; mp2});
Daniel@0 504 end
Daniel@0 505 return;
Daniel@0 506
Daniel@0 507 function cs = insertfigure(fname,boxsize,inminipage)
Daniel@0 508 global REPORT_OUTPUT_FMT
Daniel@0 509 if nargin<2, boxsize = [NaN 1]; end
Daniel@0 510 if nargin<3, inminipage = 0; end
Daniel@0 511 htmlpagewidth = 800;
Daniel@0 512 si = cell(0);
Daniel@0 513 switch REPORT_OUTPUT_FMT,
Daniel@0 514 case {'ps','pdf'},
Daniel@0 515 if ~isnan(boxsize(1)), si{end+1} = ['height=' num2str(boxsize(1)) '\textheight']; end
Daniel@0 516 if ~isnan(boxsize(2)), si{end+1} = ['width=' num2str(boxsize(2)) '\columnwidth']; end
Daniel@0 517 if length(si), si = [', ' joinstr(si, ', ', ', ')]; end
Daniel@0 518 case 'html',
Daniel@0 519 if ~isnan(boxsize(1)), si{end+1} = ['HEIGHT=' num2str(htmlpagewidth*boxsize(1))]; end
Daniel@0 520 if ~isnan(boxsize(2)), si{end+1} = ['WIDTH=' num2str(htmlpagewidth*boxsize(2))]; end
Daniel@0 521 if length(si), si = [' ' joinstr(si, ' ', ' ')]; end
Daniel@0 522 case 'txt',
Daniel@0 523 % nil
Daniel@0 524 end
Daniel@0 525 switch REPORT_OUTPUT_FMT,
Daniel@0 526 case 'ps', s = ['\epsfig{file=./' fname '.ps ' si '}'];
Daniel@0 527 case 'pdf', s = ['\includegraphics[' si ']{./' fname '.pdf}'];
Daniel@0 528 case 'html',
Daniel@0 529 fn = [fname '.' fmt2imgfmt];
Daniel@0 530 s = ['<IMG SRC="' fn '" ALIGN="center" ALT="' fname '"' si '>'];
Daniel@0 531 s = makelinkfrom(fn,s);
Daniel@0 532 case 'txt',
Daniel@0 533 s = ['[image:' fname ']'];
Daniel@0 534 end
Daniel@0 535 switch REPORT_OUTPUT_FMT,
Daniel@0 536 case {'ps','pdf'},
Daniel@0 537 if inminipage, s = inserttominipage(s,boxsize(2)); end
Daniel@0 538 case 'html',
Daniel@0 539 s = ['<CENTER>' s '</CENTER>'];
Daniel@0 540 case 'txt',
Daniel@0 541 % nil
Daniel@0 542 end
Daniel@0 543 cs = {s};
Daniel@0 544 return;
Daniel@0 545
Daniel@0 546 function str = makelinkfrom(linkto,anchor)
Daniel@0 547 global REPORT_OUTPUT_FMT
Daniel@0 548 if iscell(linkto),
Daniel@0 549 if strcmp(REPORT_OUTPUT_FMT,'html'), linkto = joinstr(linkto,'','#');
Daniel@0 550 else linkto = joinstr(linkto,'','');
Daniel@0 551 end
Daniel@0 552 end
Daniel@0 553 switch REPORT_OUTPUT_FMT,
Daniel@0 554 case 'pdf', str = ['\hyperlink{' linkto '}{' anchor '}'];
Daniel@0 555 case 'ps', str = [anchor ' (p.\pageref{' linkto '})'];
Daniel@0 556 case 'html', str = ['<a href="' linkto '">' anchor '</a>'];
Daniel@0 557 case 'txt', str = '';
Daniel@0 558 end
Daniel@0 559 return;
Daniel@0 560
Daniel@0 561 function str = makelinkto(linkname)
Daniel@0 562 global REPORT_OUTPUT_FMT
Daniel@0 563 switch REPORT_OUTPUT_FMT,
Daniel@0 564 case 'pdf',
Daniel@0 565 fmt = '\pdfdest name {%s} fit \pdfoutline goto name {%s} {%s}';
Daniel@0 566 str = sprintf(fmt,linkname,linkname,linkname);
Daniel@0 567 case 'ps', str = ['\label{' linkname '}'];
Daniel@0 568 case 'html', str = ['<a name="' linkname '"> </a>'];
Daniel@0 569 case 'txt', str = '';
Daniel@0 570 end
Daniel@0 571 return;
Daniel@0 572