Home > utility > structDispInHtml.m

structDispInHtml

PURPOSE ^

structDispInHtml: Display a structure in HTML page

SYNOPSIS ^

function structDispInHtml(struct, caption, fieldsToBeDisplayed, fieldTitles, fieldsToBeLinked, outputFile, imageFieldsToBeDisplayed)

DESCRIPTION ^

 structDispInHtml: Display a structure in HTML page
    Usage: structDispInHtml(struct, caption, fieldsToBeDisplayed, fieldTitles, fieldsToBeLinked, outputFile)

    For example:
        table=tableRead('table01.txt', 1, {'id', 'word', 'count'});
        structDispInHtml(table, 'A test table');

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function structDispInHtml(struct, caption, fieldsToBeDisplayed, fieldTitles, fieldsToBeLinked, outputFile, imageFieldsToBeDisplayed)
0002 % structDispInHtml: Display a structure in HTML page
0003 %    Usage: structDispInHtml(struct, caption, fieldsToBeDisplayed, fieldTitles, fieldsToBeLinked, outputFile)
0004 %
0005 %    For example:
0006 %        table=tableRead('table01.txt', 1, {'id', 'word', 'count'});
0007 %        structDispInHtml(table, 'A test table');
0008 
0009 %    Roger Jang, 20041001, 20081219
0010 
0011 if nargin<1; selfdemo; return; end
0012 if nargin<2, caption=[]; end
0013 if nargin<3 | isempty(fieldsToBeDisplayed), fieldsToBeDisplayed=fieldnames(struct); end
0014 if nargin<4 | isempty(fieldTitles), fieldTitles=fieldsToBeDisplayed; end
0015 if nargin<5, fieldsToBeLinked={}; end
0016 if nargin<6, outputFile=[tempname, '.htm']; end
0017 if nargin<7, imageFieldsToBeDisplayed={}; end
0018 
0019 fid=fopen(outputFile, 'w');
0020 fprintf(fid, '<html><body>\n');
0021 fprintf(fid, '<head>\n');
0022 fprintf(fid, '<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">\n');
0023 fprintf(fid, '</head>\n');
0024 fprintf(fid, '<center>\n');
0025 fprintf(fid, '<table border=1>');
0026 if ~isempty(caption)
0027     fprintf(fid, '<caption><h2>%s</h2></caption>', caption);
0028 end
0029 
0030 fprintf(fid, '<tr><th>Index\\Field');
0031 for i=1:length(fieldTitles)
0032     fprintf(fid, '<th>%s', fieldTitles{i});
0033 end
0034 
0035 for i=1:length(struct)
0036     fprintf(fid, '<tr><td>&nbsp;%d', i);
0037     for j=1:length(fieldsToBeDisplayed)
0038         data=getfield(struct, {i}, fieldsToBeDisplayed{j});
0039         if isstr(data)
0040             if any(strcmp(fieldsToBeDisplayed{j}, fieldsToBeLinked))
0041                 fprintf(fid, '<td><a href="%s">&nbsp;%s</a>', data, data);
0042             elseif any(strcmp(fieldsToBeDisplayed{j}, imageFieldsToBeDisplayed))
0043                 fprintf(fid, '<td><img src="%s">', data);
0044             else
0045                 fprintf(fid, '<td>&nbsp;%s', data);
0046             end
0047         elseif iscell(data)
0048             fprintf(fid, '<td>&nbsp;%s', cell2str(data));
0049         else
0050             fprintf(fid, '<td>&nbsp;%s', mat2str(data, 8));
0051         end
0052     end
0053     fprintf(fid, '\n');
0054 end
0055 
0056 fprintf(fid, '</table>\n');
0057 fprintf(fid, '</center>\n');
0058 fprintf(fid, '</body></html>\n');
0059 fclose(fid);
0060 
0061 dos(['start ', outputFile]);
0062 
0063 % ====== Self demo
0064 function selfdemo
0065 testTable=dir;
0066 feval(mfilename, testTable, 'Test table', [], {'name'});

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003