HTML - Create HTML formatted string. Important note: Matlab UITables are buggy! Therefore, HTML formatted table cells will not be rendered satisfactorily (e.g. you will experience problems with alignment and cell width). This function does not try to compensate for these bugs. USAGE out = HTML(in,format) in string, number or cell array to format format color, weight and angle information NOTE The format string is a concatenation of one or more of the following items: B boldface I italics #nnnnnn HTML color code (e.g. #f1c27a) EXAMPLES % Create a table figure with two items, % the first one in bright red bold characters m = { HTML('Total','B#ff0000'),200 }; TableFigure('Results',m); % Create a table figure with two items, % both in bright red bold characters m = HTML({'Total',200},'B#ff0000'); TableFigure('Results',m); SEE See also TableFigure.
0001 function out = HTML(in,format) 0002 0003 %HTML - Create HTML formatted string. 0004 % 0005 % Important note: Matlab UITables are buggy! Therefore, HTML formatted table 0006 % cells will not be rendered satisfactorily (e.g. you will experience problems 0007 % with alignment and cell width). This function does not try to compensate for 0008 % these bugs. 0009 % 0010 % USAGE 0011 % 0012 % out = HTML(in,format) 0013 % 0014 % in string, number or cell array to format 0015 % format color, weight and angle information 0016 % 0017 % NOTE 0018 % 0019 % The format string is a concatenation of one or more of the following items: 0020 % 0021 % B boldface 0022 % I italics 0023 % #nnnnnn HTML color code (e.g. #f1c27a) 0024 % 0025 % EXAMPLES 0026 % 0027 % % Create a table figure with two items, 0028 % % the first one in bright red bold characters 0029 % 0030 % m = { HTML('Total','B#ff0000'),200 }; 0031 % TableFigure('Results',m); 0032 % 0033 % % Create a table figure with two items, 0034 % % both in bright red bold characters 0035 % 0036 % m = HTML({'Total',200},'B#ff0000'); 0037 % TableFigure('Results',m); 0038 % 0039 % SEE 0040 % 0041 % See also TableFigure. 0042 0043 % Copyright (C) 2013 by Michaƫl Zugaro 0044 % 0045 % This program is free software; you can redistribute it and/or modify 0046 % it under the terms of the GNU General Public License as published by 0047 % the Free Software Foundation; either version 3 of the License, or 0048 % (at your option) any later version. 0049 0050 % Check parameters 0051 if nargin < 2, 0052 error('Incorrect number of parameters (type ''help <a href="matlab:help HTML">HTML</a>'' for details).'); 0053 end 0054 if ~iscell(in), 0055 in = {in}; 0056 inputIsCell = 0; 0057 else 0058 inputIsCell = 1; 0059 end 0060 for i = 1:length(in(:)), 0061 if ~ischar(in{i}) && ~isscalar(in{i}), 0062 error('Incorrect string or number (type ''help <a href="matlab:help HTML">HTML</a>'' for details).'); 0063 end 0064 end 0065 if ~ischar(format), 0066 error('Incorrect format (type ''help <a href="matlab:help HTML">HTML</a>'' for details).'); 0067 end 0068 0069 format = lower(format); 0070 out = in; 0071 0072 % Parse format string 0073 r = regexp(format,'([bi]*)((#[0-9a-f]{6})*)([bi]*)','tokens'); 0074 if isempty(r), return; end 0075 r = r{1}; 0076 if ~strcmp(format,[r{:}]), 0077 error('Incorrect format (type ''help <a href="matlab:help HTML">HTML</a>'' for details).'); 0078 end 0079 0080 global color style; 0081 0082 % Color 0083 c = r{2}; 0084 if isempty(c), 0085 color = ''; 0086 else 0087 color = ['color: ' c ';']; 0088 end 0089 0090 % Style 0091 s = [r{[1 3]}]; 0092 style = ''; 0093 if ~isempty(s), 0094 if any(s=='i'), 0095 style = 'font-angle: italic;'; 0096 end 0097 if any(s=='b'), 0098 style = [style 'font-weight: bold;']; 0099 end 0100 end 0101 0102 if isempty([color style]), return; end 0103 0104 % Output HTML string 0105 out = cellfun(@FormatCell,in,'uniformoutput',0); 0106 0107 if ~inputIsCell, 0108 out = out{:}; 0109 end 0110 0111 0112 function y = FormatCell(x) 0113 0114 global color style; 0115 0116 if isnumeric(x), 0117 x = num2str(x); 0118 align = 'text-align:right;'; 0119 else 0120 align = ''; 0121 end 0122 y = ['<html><span style="' color style align '">' x '</span></html>'];