【问题标题】:How to document object-oriented MATLAB code? [closed]如何记录面向对象的 MATLAB 代码? [关闭]
【发布时间】:2011-01-25 22:19:24
【问题描述】:

我正在使用面向对象的 MATLAB 编写一个相当大的应用程序,这让我开始思考如何记录代码。如果这是 C,我会使用 Doxygen。对于 Java,我会使用 JavaDoc。对于类和方法文档的外观以及应包含的内容,两者都已达成一致的标准。

但是 MATLAB 代码呢?我在 TMW 自己的课程中看到的最多的是课程顶部的一两句话,我找不到任何专门用于记录大型 MATLAB 应用程序的主题。

那么您如何记录您的 MATLAB 类?任何特定的样式问题或其他工具?

【问题讨论】:

    标签: matlab documentation


    【解决方案1】:

    我意识到这个问题已经过时了,但为了 Google 的利益:Matlab 有一个内置的功能。您以某种风格(如 JavaDoc)编写 cmets,它们会被帮助和 doc 函数拾取。它可用于记录类、属性和方法。它令人惊讶地完整,但有点挑剔。文档在这里:

    http://www.mathworks.com/help/matlab/creating-help.html

    【讨论】:

    • 这正是我想要的,谢谢。
    【解决方案2】:

    我用以下方式记录我的 oo 代码:

    1. 在包含“classdef”的文件的开头,我总结了类的作用和典型用法。我还详细解释了属性,并添加了每个方法的 1 句描述。
    2. 在每个属性定义之后,我添加一个关于它的解释性句子(在同一行)
    3. 每个方法都像函数一样记录在案,即它具有 H1 行、概要以及输入和输出参数的说明。

    当您调用“doc myClass”时,您会在开头看到 (1),然后是您在 (2) 中添加的句子所解释的属性列表以及显示 H1 行和如果您单击链接,其余的帮助 (3)。

    此外,我所有的类都是一个通用超类的子类,它实现(除其他外)调用 doc(class(obj)) 的方法“帮助”,它允许我从类的每个实例中调出帮助。

    例子

    %# MYCLASS is a sample class
    %# All this text will show up at the top of the help if you call 'doc myClassName'
    %#
    %# myClass is an example for documentation. It implements the following properties and methods:
    %# PROPERTIES
    %#    myProp - empty sample property (some more explanation could follow here)
    %#
    %# METHODS
    %#    myMethod - sample method that calls doc
    %#
    
    classdef myClass
    
    properties
        myProp = []; %# empty sample property
    end %# properties
    
    methods
    
    %%# MYMETHOD -- use %% so that you can easily navigate your class file
    function myMethod(obj)
    %#MYMETHOD calls up the help for the object
    %#
    %# SYNOPSIS myMethod(obj)
    %# INPUT obj: the object
    %# OUTPUT none
    %#
       try
          doc(class(obj))
       catch
          help(class(obj))
       end
       end %#myMethod
    end %#methods
    
    end %#myClass
    

    编辑 1 如果您想要一个漂亮的 html 文档,您还可以使用 m2html 为您生成它。 M2html 会收集帮助文本,它甚至可以做依赖图。

    编辑 2 虽然 m2html 可以很好地记录标准 Matlab 代码,但它没有对类的特定支持。这意味着您将方法作为链接到类中的“子函数”获得,但您不会获得像使用 Doxygen 或使用内置文档浏览器获得的那样好的摘要。

    【讨论】:

    • m2html 能否为 Matlab 中的新 classdef 语法生成文档?我在文档中找不到任何提示。
    • @Jonas:是的,m2html 很酷,但不是最适合 matlab 类的。此外,您不能排除您不想索引和记录的某些目录!除了doxygen,到目前为止还有什么更好的方法吗?谢谢
    • 如果您有多个输入和/或输出怎么办?
    • 那就是myMethod(obj, other input)
    【解决方案3】:

    尝试使用 Sphinxmatlabdomain 扩展名。 Sphinx 是一个Python 包,它使用ReStructuredText (rst) markup 自动记录代码。扩展 sphinxcontrib-matlabdomain 允许使用 Sphinx 在其文档字符串中识别的第一个标记的 MATLAB 代码的自动文档化。将错误和建议发送至issue tracker on BitBucket

    例如my_project/my_fun.m中的如下代码:

    function [outputs] = my_fun(args)
    % MY_FUN does really cool stuff
    % [OUTPUTS] = MY_FUN(ARGS)
    %
    % :param args: Input arguments
    % :type args: cell array
    % :returns: outputs
    % :raises: :exc:`my_project.InvalidInput`
    
    code ...
    end
    

    将记录在这样的第一个文件中:

    .. _my-project
    
    My Project
    ==========
    .. automodule:: my_project
    
        This folder contains all the functions and classes for my project.
    
    My Function
    -----------
    .. autofunction:: my_fun
    

    并且会生成像this blog post 上显示的 html(或 pdf、latex 和许多其他)。

    【讨论】:

      【解决方案4】:

      FileExchange 上有一个用于 M-Files 的 Doxygen-Adapter,请参阅 http://www.mathworks.com/matlabcentral/fileexchange/25925-using-doxygen-with-matlab

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-25
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多