Matlab代碼編輯器具有代碼釋出功能,如下圖,當編輯好代碼後,點選Publish按鈕可以釋出html網頁格式的代碼使用說明。
從上面的圖中可以看到,釋出功能可以控制字型(黑體、斜體、等寬字型),添加連結、latex公式、清單、代碼等,功能友善實用,具體可以參考幫助中的介紹,如下表所示。
RESULT IN OUTPUT | EXAMPLE OF CORRESPONDING FILE MARKUP |
Sections and Section Titles 使用%%或%%%來分章節,緊随之後的%為介紹文本 | |
Text Formatting _括起來的表示斜體,*括起來的黑體,|括起來的為等寬字型 文本後加(TM),則TM為上标;加(R),則為文本加R商标 | |
Bulleted and Numbered Lists 以*加空格開頭的為符号清單,以#加空格開頭的為序号清單 | |
Text and Code Blocks 另起一行以兩個空格開頭的文本以預置格式顯示 另起一行以三個空格開頭的文本以代碼格式顯示 | |
External File Content 以<include>括起來的檔案直接顯示其代碼内容 | |
External Graphics 以<<>>括起來的為添加圖檔 | |
Image Snapshot snapnow為捕捉figure快照并顯示 | |
LaTeX Equations 添加latex公式 | |
Hyperlinks 添加網址連結或調用matlab代碼執行 | |
HTML Markup 添加html内容以顯示,隻在釋出為html網頁時有效 | |
LaTeX Markup 添加latex内容,隻在釋出為latex時有效 | |
借助Matlab這個功能,可以用來制作個人首頁,如下面的代碼
檔案名tempweb.m:
%% *春秋*
%
% <<chunqiu.jpg>>
% 位址:
% 安徽省
% 部落格:
% <http://blog.sina.com.cn/gjchunqiu 新浪部落格>, <https://chunqiu.blog.ustc.edu.cn USTC部落格>
%% 教育背景
% * 高中
% * 大學
fprintf('<html><p align="left"><font face="Times New Roman">[<a href="#Top" target="_self">Go Top</a>]</font></p></html>\n')
%% 研究興趣
% * *視覺測量,目标跟蹤*
% * *信号處理*
%% 研究項目
% <html>
% <table width="500">
% <tr>
% <td>項目名稱</td>
% <td>項目簡介</td>
% <td>示例</td>
% </tr>
% <td>視覺系統</td>
% <td>視覺系統介紹</td>
% <td><div align="center"><img src="chunqiu.jpg" width="100"></div></td>
% </table>
% </html>
fprintf('<html><p align="left"><font face="Times New Roman">[<a href="#Top" target="_self">Go Top</a>]</font></p></html>\n')
釋出後得到的網頁如下
預設情況下,釋出的網頁會顯示出matlab代碼。為了不讓這些代碼顯示出來,可以設定publish選項取消顯示:點選Publish下的Edit Publishing Options...,設定Include code為false。
設定好後再釋出,得到的結果如下,可以看到不再包含matlab代碼:
另外,這些設定還可以通過代碼的方式執行,如
檔案名publishweb.m
% 生成網頁
clc
clear
close all
%% 網頁檔案名
filename = 'tempweb';
% 網頁釋出
options.showCode = false;
% options.stylesheet = 'html.xsl';
publish([filename,'.m'],options);
% 檢視網頁效果
web(['html/', filename, '.html'])
直接運作此代碼即可釋出網頁(非點選publish按鈕)。
預設情況下,Matlab會以通用格式生成網頁,第一部分為網頁介紹,然後添加目錄,繼而依次顯示後續部分,此預設格式由toolbox\matlab\codetools\private\mxdom2simplehtml.xsl檔案控制,如字型大小、顔色等。
為了制作個性化的個人首頁,可以修改此檔案,重新調整字型大小、目錄顯示等,将此檔案命名為html.xsl,儲存在publishweb.m同目錄下,修改publishweb.m檔案為:
options.stylesheet = 'html.xsl';
tempweb.m檔案修改為:
tabledata{1,1} = '項目名稱';
tabledata{1,2} = '項目簡介';
tabledata{1,3} = '示例';
tabledata{2,1} = '視覺系統';
tabledata{2,2} = {'自然基金', ...
'項目介紹,項目介紹,項目介紹,項目介紹,項目介紹,項目介紹。',...
'<b>關鍵詞:</b>目标跟蹤'};
tabledata{2,3} = {'chunqiu.jpg', 200}; % 以指定的寬度顯示圖檔
CreateTable(tabledata);
其中CreateTable函數為
% 使用fprintf輸出建立表格
function CreateTable(tabledata)
[rows, cols] = size(tabledata);
fprintf('<html>\n');
fprintf('<table>\n'); % width="1300"
for i = 1:rows
fprintf('<tr>\n');
for j = 1:cols
tempstr = '';
if ischar(tabledata{i,j}) % 如果是字元串
if (i == 1) % 如果是待顯示字元串
% 第一行居中輸出
tempstr = ['<div align="center">', tabledata{i,j}, '</div>'];
elseif (j == 1 && i > 1)
% 第一列文字加粗居中輸出
tempstr = ['<div align="center"><b>' tabledata{i,j} ' </b></div>'];
else % 其他文字加空格輸出
tempstr = [' ' tabledata{i,j}];
end
elseif iscellstr(tabledata{i,j}) % 如果是字元串元胞
tempstr = MergeCellStr(tabledata{i,j});
elseif iscell(tabledata{i,j}) % 如果是圖檔
if exist(['html/', tabledata{i,j}{1}],'file') == 2 % 如果圖檔存在
tempstr = sprintf('<div align="center"><img src="%s" width="%d"></div>', tabledata{i,j}{1}, tabledata{i,j}{2});
end
fprintf('<td>%s</td>\n', tempstr);
end
fprintf('</tr>\n');
end
fprintf('</table>\n');
fprintf('</html>\n');
%% 合并字元串元胞
function str = MergeCellStr(strcell)
str = [' ', strcell{1}];
for i = 2:length(strcell)
str = [str, '<br> ', strcell{i}]; % 為空格
運作publishweb.m,得到如下效果
注意:由于修改後的xsl會自動添加[go top]置頂連結,是以可以将tempweb.m檔案中不需要額外添加置頂連結。
另外,Matlab還提供了一個MATLAB Report Generator工具箱,它提供了GUI可以完成html、doc、ppt等格式的報告釋出,功能強大。
html.xsl檔案:
<?xml version="1.0" encoding="utf-8"?>
<!--
This is an XSL stylesheet which converts mscript XML files into HTML.
Use the XSLT command to perform the conversion.
Copyright 1984-2012 The MathWorks, Inc.
-->
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> <!ENTITY reg "®"> ]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"
exclude-result-prefixes="mwsh">
<xsl:output method="html"
indent="no"
doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/> <!-- encoding="GB2312" -->
<xsl:strip-space elements="mwsh:code"/>
<!--配置網頁的标題-->
<xsl:variable name="title"><!--title變量,即網頁标題-->
<xsl:variable name="dTitle" select="//steptitle[@style='document']"/><!--dTitle臨時變量-->
<xsl:choose>
<xsl:when test="$dTitle"><xsl:value-of select="$dTitle"/></xsl:when><!--使用dTitle作為标題-->
<xsl:otherwise><xsl:value-of select="mscript/m-file"/></xsl:otherwise><!--使用檔案名作為标題-->
</xsl:choose>
</xsl:variable>
<xsl:template match="mscript">
<html>
<!-- 配置head -->
<head>
<!--這句話作為注釋出現在生成html中-->
<xsl:comment>
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
</xsl:comment>
<!--配置html标題-->
<title><xsl:value-of select="$title"/></title><!--使用title變量作為網頁标題-->
<meta name="generator">
<xsl:attribute name="content">MATLAB <xsl:value-of select="version"/> by GJ</xsl:attribute>
</meta>
<link rel="schema.DC" href="http://purl.org/dc/elements/1.1/" />
<meta name="DC.date">
<xsl:attribute name="content"><xsl:value-of select="date"/></xsl:attribute>
</meta>
<meta name="DC.source">
<xsl:attribute name="content"><xsl:value-of select="m-file"/>.m</xsl:attribute>
<!-- 調用下面的stylesheet template -->
<xsl:call-template name="stylesheet"/>
</head>
<!-- 配置body -->
<body>
<!-- 調用下面的header template,實際為空 -->
<xsl:call-template name="header"/>
<div class="content">
<!-- Determine if there should be an introduction section. 判斷是否應該有introduction部分 -->
<xsl:variable name="hasIntro" select="count(cell[@style = 'overview'])"/>
<!-- If there is an introduction, display it. 如果有,顯示它 -->
<xsl:if test = "$hasIntro">
<h1><xsl:apply-templates select="cell[1]/steptitle"/></h1>
<xsl:comment>introduction</xsl:comment>
<xsl:apply-templates select="cell[1]/text"/>
<!-- There can be text output if there was a parse error. -->
<xsl:apply-templates select="cell[1]/mcodeoutput"/>
<xsl:comment>/introduction</xsl:comment>
</xsl:if>
<xsl:variable name="body-cells" select="cell[not(@style = 'overview')]"/>
<!-- Include contents if there are titles for any subsections. 如果每個部分都有标題,則顯示目錄 -->
<xsl:if test="count(cell/steptitle[not(@style = 'document')])">
<xsl:call-template name="gjcontents">
<xsl:with-param name="body-cells" select="$body-cells"/>
</xsl:call-template>
<!-- Loop over each cell 對每一個cell循環執行 -->
<xsl:for-each select="$body-cells">
<!-- Title of cell -->
<xsl:if test="steptitle">
<xsl:variable name="headinglevel">
<xsl:choose>
<xsl:when test="steptitle[@style = 'document']">h1</xsl:when>
<xsl:otherwise>h2</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$headinglevel}">
<xsl:apply-templates select="steptitle"/>
<xsl:if test="not(steptitle[@style = 'document'])">
<a>
<xsl:attribute name="name">
<xsl:value-of select="position()"/>
</xsl:attribute>
</a>
</xsl:if>
</xsl:element>
</xsl:if>
<!-- Contents of each cell -->
<xsl:apply-templates select="text"/>
<xsl:apply-templates select="mcode-xmlized"/>
<xsl:apply-templates select="mcodeoutput|img"/>
<!-- 到頂部連結 -->
[<a href="#Top" target="_self">go top</a>]
<p></p>
</xsl:for-each>
<xsl:call-template name="footer"/>
</div>
<!-- 顯示原始代碼 -->
<xsl:apply-templates select="originalCode"/>
</body>
</html>
</xsl:template>
<xsl:template name="stylesheet">
<style type="text/css">
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}
body{line-height:1}
ol,ul{list-style:none}
blockquote,q{quotes:none}
blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}
ins{text-decoration:none}del{text-decoration:line-through}
table{border-collapse:collapse;border-spacing:0}
html { min-height:100%; margin-bottom:1px; }
html body { height:100%; margin:0px; font-family:Times New Roman, Arial, Helvetica, sans-serif; font-size:18px; color:#000; line-height:140%; background:#fff none; overflow-y:scroll; }
html body td { vertical-align:middle; text-align:left; }
h1 { padding:0px; margin:0px 0px 25px; font-family:楷體, Times New Roman, Arial, Helvetica, sans-serif; font-size:2.5em; color:#d55000; line-height:100%; font-weight:normal; }
h2 { padding:0px; margin:10px 0px 8px; font-family:Times New Roman, Arial, Helvetica, sans-serif; font-size:1.2em; color:#000; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }
h3 { padding:0px; margin:0px 0px 5px; font-family:Times New Roman, Arial, Helvetica, sans-serif; font-size:1.1em; color:#000; font-weight:bold; line-height:140%; }
a { color:blue; text-decoration:underline; }
a:hover { color:red; text-decoration:underline; }<!-- 滑鼠移上去的顔色 -->
a:visited { color:purple; text-decoration:underline; }<!-- 通路過的顔色 -->
p { padding:0px; margin:0px 0px 5px; }
img { padding:0px; margin:0px 0px 0px; border:none; }
p img, pre img, tt img, li img, h1 img, h2 img { margin-bottom:0px; }
ul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }
ul li { padding:0px; margin:0px 0px 7px 0px; }
ul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }
ul li ol li { list-style:decimal; }
ol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }
ol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }
ol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }
ol li ol li { list-style-type:lower-alpha; }
ol li ul { padding-top:7px; }
ol li ul li { list-style:square; }
.content { font-size:1.2em; line-height:140%; padding: 20px; }
pre, code { font-size:20px; }
tt { font-size: 1.2em; }
pre { margin:-10px 0px 10px; }
pre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }
pre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }
pre.error { color:red; }
@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }
span.keyword { color:#0000FF }
span.comment { color:#228B22 }
span.string { color:#A020F0 }
span.untermstring { color:#B20000 }
span.syscmd { color:#B28C00 }
.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }
.footer p { margin:0px; }
.footer a { color:#878787; }
.footer a:hover { color:#878787; text-decoration:underline; }
.footer a:visited { color:#878787; }
table th { padding:7px 5px; text-align:left; vertical-align:middle; border: 1px solid #d6d4d4; font-weight:bold; }
table td { padding:7px 5px; text-align:left; vertical-align:top; border:2px solid #C0DCC0; }
</style>
<!-- Header 頁眉 -->
<xsl:template name="header">
<!-- Footer 頁腳 -->
<xsl:template name="footer">
<p class="footer">
<xsl:value-of select="copyright"/>
<!--<a href="https://chunqiu.blog.ustc.edu.cn">Published with MATLAB® R<xsl:value-of select="release"/> by GJ, <xsl:value-of select="date"/></a>-->
Published with MATLAB® R<xsl:value-of select="release"/> on <xsl:value-of select="date"/>
</p>
<!-- Matlab配置的目錄 -->
<xsl:template name="contents">
<xsl:param name="body-cells"/>
<h2>目錄</h2>
<div>
<ul>
<xsl:for-each select="$body-cells">
<xsl:if test="./steptitle">
<li>
<a>
<xsl:attribute name="href">#<xsl:value-of select="position()"/></xsl:attribute><xsl:apply-templates select="steptitle"/>
</a>
</li>
</xsl:for-each>
</ul>
</div>
</xsl:template>
<!-- GJ配置的目錄 -->
<xsl:template name="gjcontents">
<!-- <a><xsl:attribute name="href">Top</xsl:attribute></a> -->
<xsl:for-each select="$body-cells">
<xsl:if test="./steptitle">
[<a><xsl:attribute name="href">#<xsl:value-of select="position()"/></xsl:attribute><xsl:apply-templates select="steptitle"/></a>]
</xsl:if>
</xsl:for-each>
<!-- HTML Tags in text sections -->
<xsl:template match="p">
<p><xsl:apply-templates/></p>
<xsl:template match="ul">
<div><ul><xsl:apply-templates/></ul></div>
<xsl:template match="ol">
<div><ol><xsl:apply-templates/></ol></div>
<xsl:template match="li">
<li><xsl:apply-templates/></li>
<xsl:template match="pre">
<xsl:when test="@class='error'">
<pre class="error"><xsl:apply-templates/></pre>
</xsl:when>
<xsl:otherwise>
<pre><xsl:apply-templates/></pre>
</xsl:otherwise>
<xsl:template match="b">
<b><xsl:apply-templates/></b>
<xsl:template match="i">
<i><xsl:apply-templates/></i>
<xsl:template match="tt"> <!-- 重定義為下劃線 -->
<u><xsl:apply-templates/></u>
<xsl:template match="a">
<a>
<xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:apply-templates/>
</a>
<xsl:template match="html">
<xsl:value-of select="@text" disable-output-escaping="yes"/>
<xsl:template match="latex"/>
<!-- Detecting M-Code in Comments-->
<xsl:template match="text/mcode-xmlized">
<pre class="language-matlab"><xsl:apply-templates/><xsl:text><!-- g162495 -->
</xsl:text></pre>
<!-- Code input and output -->
<xsl:template match="mcode-xmlized">
<pre class="codeinput"><xsl:apply-templates/><xsl:text><!-- g162495 -->
<xsl:template match="mcodeoutput">
<xsl:when test="concat(substring(.,0,7),substring(.,string-length(.)-7,7))='<html></html>'">
<xsl:value-of select="substring(.,7,string-length(.)-14)" disable-output-escaping="yes"/>
<pre>
<xsl:attribute name="class">
<xsl:value-of select="@class"/>
</xsl:attribute>
<xsl:apply-templates/>
</pre>
</xsl:template>
<!-- Figure and model snapshots and equations -->
<xsl:template match="img[@class='equation']">
<img>
<xsl:attribute name="src"><xsl:value-of select="@src"/></xsl:attribute>
<xsl:attribute name="alt"><xsl:value-of select="@alt"/></xsl:attribute>
</img>
<xsl:template match="img">
<img vspace="5" hspace="5">
<xsl:text> </xsl:text>
<!-- Stash original code in HTML for easy slurping later. 原始代碼 -->
<xsl:template match="originalCode">
<xsl:variable name="xcomment">
<xsl:call-template name="globalReplace">
<xsl:with-param name="outputString" select="."/>
<xsl:with-param name="target" select="'--'"/>
<xsl:with-param name="replacement" select="'REPLACE_WITH_DASH_DASH'"/>
</xsl:call-template>
</xsl:variable>
<xsl:comment>
##### SOURCE BEGIN #####
<xsl:value-of select="$xcomment"/>
##### SOURCE END #####
</xsl:comment>
<!-- Colors for syntax-highlighted input code -->
<xsl:template match="mwsh:code">
<xsl:apply-templates/>
<xsl:template match="mwsh:keywords">
<span class="keyword"><xsl:value-of select="."/></span>
<xsl:template match="mwsh:strings">
<span class="string"><xsl:value-of select="."/></span>
<xsl:template match="mwsh:comments">
<span class="comment"><xsl:value-of select="."/></span>
<xsl:template match="mwsh:unterminated_strings">
<span class="untermstring"><xsl:value-of select="."/></span>
<xsl:template match="mwsh:system_commands">
<span class="syscmd"><xsl:value-of select="."/></span>
<!-- Footer information -->
<xsl:template match="copyright">
<xsl:value-of select="."/>
<xsl:template match="revision">
<!-- Search and replace -->
<!-- From http://www.xml.com/lpt/a/2002/06/05/transforming.html -->
<xsl:template name="globalReplace">
<xsl:param name="outputString"/>
<xsl:param name="target"/>
<xsl:param name="replacement"/>
<xsl:when test="contains($outputString,$target)">
<xsl:value-of select=
"concat(substring-before($outputString,$target),$replacement)"/>
<xsl:call-template name="globalReplace">
<xsl:with-param name="outputString"
select="substring-after($outputString,$target)"/>
<xsl:with-param name="target" select="$target"/>
<xsl:with-param name="replacement"
select="$replacement"/>
</xsl:call-template>
<xsl:value-of select="$outputString"/>