天天看点

Matlab制作个人主页

 Matlab代码编辑器具有代码发布功能,如下图,当编辑好代码后,点击Publish按钮可以发布html网页格式的代码使用说明。

    从上面的图中可以看到,发布功能可以控制字体(黑体、斜体、等宽字体),添加链接、latex公式、列表、代码等,功能方便实用,具体可以参考帮助中的介绍,如下表所示。

RESULT IN OUTPUT EXAMPLE OF CORRESPONDING FILE MARKUP

Sections and Section Titles

使用%%或%%%来分章节,紧随之后的%为介绍文本

%% SECTION TITLE
% DESCRIPTIVE TEXT      
%%% SECTION TITLE WITHOUT SECTION BREAK
% DESCRIPTIVE TEXT      

Text Formatting

_括起来的表示斜体,*括起来的黑体,|括起来的为等宽字体

文本后加(TM),则TM为上标;加(R),则为文本加R商标

% _ITALIC TEXT_      
% *BOLD TEXT*      
% |MONOSPACED TEXT|      
% Trademarks:
% TEXT(TM)      
% TEXT(R)      

Bulleted and Numbered Lists

以*加空格开头的为符号列表,以#加空格开头的为序号列表

%% Bulleted List
%
% * BULLETED ITEM 1
% * BULLETED ITEM 2
%      
%% Numbered List
%
% # NUMBERED ITEM 1
% # NUMBERED ITEM 2
%      

Text and Code Blocks

另起一行以两个空格开头的文本以预置格式显示

另起一行以三个空格开头的文本以代码格式显示

%% 
% 
%  PREFORMATTED
%  TEXT
%      
%% MATLAB(R) Code
% 
%   for i = 1:10
%       disp x
%   end
%      

External File Content

以<include>括起来的文件直接显示其代码内容

% 
% <include>filename.m</include>
%      

External Graphics

以<<>>括起来的为添加图片

% 
% <<FILENAME.PNG>>
%      

Image Snapshot

snapnow为捕捉figure快照并显示

​snapnow;​

LaTeX Equations

添加latex公式

%% Inline Expression
% $x^2+e^{\pi i}$      
%% Block Equation
%  eπi+1=0      

Hyperlinks

添加网址链接或调用matlab代码执行

% <http://www.mathworks.com MathWorks>      
% <matlab:FUNCTION DISPLAYED_TEXT>      

HTML Markup

添加html内容以显示,只在发布为html网页时有效

%
% <html>
% <table border=1><tr>
% <td>one</td>
% <td>two</td></tr></table>
% </html>
%      

LaTeX Markup

添加latex内容,只在发布为latex时有效

%% LaTeX Markup Example
% <latex>
% \begin{tabular}{|r|r|}
% \hline $n$&$n!$\\ 
% \hline 1&1\\ 2&2\\ 3&6\\ 
% \hline
% \end{tabular}
% </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"/>