简体   繁体   English

如何在Octave中记录功能?

[英]How to document a function in Octave?

The publish function in MATLAB works for both scripts and functions, while the one for Octave works only for scripts: if I enter MATLAB中publish函数同时适用于脚本和函数,而Octavepublish函数仅适用于脚本:如果我输入

publish myFunc.m

Octave gave 八度给

error: publish: Only Octave script files can be published. 错误:发布:只能发布八度脚本文件。

Am I able to publish a function in Octave? 我可以在Octave中发布功能吗? If yes, how? 如果是,怎么办?

You can use Octave Forge generate_html package which is meant to generate html of individual functions. 您可以使用Octave Forge generate_html包,该包用于生成各个函数的html。 It is mostly used to generate the documentation of Octave Forge packages, and its defaults reflect that, but you could add any style you want. 它主要用于生成Octave Forge软件包的文档,其默认设置反映了这一点,但是您可以添加所需的任何样式。

This package will use the same help text that the help function in Octave sees which is the first block of comments in the file that does not start by Copyright or Author . 此程序包将使用与Octave中的help功能所看到的帮助文本相同的帮助文本,该帮助文本是文件中第一行注释(不是以CopyrightAuthor开头)。 You can have it in plain text but for nicer formatting, you can use Texinfo . 您可以使用纯文本格式,但是要获得更好的格式,可以使用Texinfo In that case, the first line of the help text should be -*- texinfo -*- . 在这种情况下,帮助文本的第一行应为-*- texinfo -*- There is a page on the Octave wiki with tips on how to write nice help text including a short section on Texinfo syntax (the actual Texinfo manual can be a bit overwhelming). 在Octave Wiki上有一个页面,其中包含有关如何编写好的帮助文本的提示,其中包括有关Texinfo语法的一小部分(实际的Texinfo手册可能有点让人不知所措)。

In addition to the help text, the generate_html package also identifies %!demo blocks and generates a section with the demo code and output it generates, including figures. 除了帮助文本之外, generate_html包还标识%!demo块,并使用演示代码和其生成的输出(包括数字)生成一个部分。

The best way to see how help text and demo blocks work in Octave is to check the source (as @Andy pointed out in the comments). 查看帮助文本和演示块在Octave中如何工作的最好方法是检查源(如@Andy在评论中指出的那样)。 For example, see the source for inpolygon (scroll to the bottom to find the %!demo blocks, right before %!test and %!error ). 例如,请参见inpolygon来源 (滚动到底部以找到%!demo块,就在%!test%!error )。 The generate_html package generates this page (note the Demonstration blocks). generate_html包将生成此页面 (请注意Demonstration块)。

This is a "multiple questions in one" question, making lots of assumptions in between, so let's address those first: 这是一个“多个问题合而为一”的问题,在两者之间作了许多假设,因此让我们首先解决这些问题:

1. I'll start by the question in the comment, since that's the easiest: Matlab's publisher is not a code documentation tool. 1.我将从评论中的问题开始,因为这是最简单的:Matlab的发布者不是代码文档工具。 It's a "make a quick report that includes both text and code to show at your supervisor meeting or write a quick point in a blog" tool. 这是一个“制作包含文本和代码的快速报告,以在主管会议上显示或在博客中写一个快速要点”工具。 So the link you point to is simply irrelevant in this case, since that talks about documentation for matlab code. 因此,在这种情况下,您指向的链接是无关紧要的,因为它涉及的是matlab代码的文档。

2. The fact that matlab's publisher also "works for functions", particularly given the first point here, should be considered to be more of a bug than a feature, or at most as a trivial undocumented extension. 2. matlab的发布者也“为功能而工作”的事实,尤其是此处提到的第一点,应被视为更多的是错误而不是功能,或者至多是一个琐碎的未记录的扩展。 If you look at the matlab documentation for the publish command, you'll see it expects a filename, not a function with arguments, and the documentation only talks about scripts and makes no mention of 'function' compatibility. 如果您查看matlab文档中的publish命令,您会看到它需要一个文件名,而不是带有参数的函数,并且该文档仅讨论脚本,而没有提及“函数”的兼容性。

3. Furthermore, even if you did want to use publisher as a "documentation tool", this is counterintuitive for functions in this format, since you need to provide arguments in order for publisher to work (which will not be shown in the actual report), you'll need a modified version that displays intermediate calculations (as opposed to your normal version which presumably does not), and the function just spews an ugly ans= blabla at the end of the report. 3.此外,即使您确实想将发布者用作“文档工具”,对于这种格式的功能来说,这也是违反直觉的,因为您需要提供参数才能使发布者工作(在实际报告中不会显示) ),则需要一个修改后的版本来显示中间计算结果(而不是普通版本,而正常情况下可能不会),并且该函数只会在报告末尾显示一个丑陋的ans= blabla If your end goal is documentation, it might be best to write a bespoke script for this anyway, showing proper usage and examples, like matlab does in its actual documentation. 如果您的最终目标是文档,那么最好还是为此编写一个定制脚本,显示正确的用法和示例,就像matlab在其实际文档中所做的那样。


Having said all that, yes, there is a 'cheat' you can do to include a function's code in your published report , which is that, in octave (also matlab since R2016b), functions can be defined locally. 说了这么多, 是的,您可以做一个“骗子”,以便在已发布的报告中包含函数的代码 ,这就是说,在倍频程中(自R2016b起也是matlab),可以在本地定义函数。 A .m file that only contains function definitions is interpreted as a function file, but if there are other non-function-declaration instructions preceding the function definitions (other than comments), then it is seen as a script. 仅包含函数定义的.m文件被解释为函数文件,但是如果在函数定义(注释除外)之前还有其他非函数声明指令,则将其视为脚本。 So if you publish this script, you effectively get a published report with function code in it: 因此,如果发布此脚本,则可以有效地获得包含功能代码的已发布报告:

%% Adding function
% This function takes an input and adds 5 to it.

%% Example inputs
In = 10;

%% The function itself
% Marvel at its beauty!
function Out = myfun(In)

  %% Here is where the addition takes place.
  % It is a beautiful addition
  Out = In + 5;

end

%% Example use
Out = myfun(In)

(If you're not happy about having to create a 'wrapper script' manually, you can always create your own wrapper function that does this automatically). (如果您对手动创建“包装器脚本”不满意,则始终可以创建自己的自动执行包装器功能)。

However, both the matlab and octave publishers are limited tools by design . 但是,matlab和octave发布者在设计上都是受限制的工具。 Like I said earlier, it's more of a "quick report to show numbers and plots to your supervisor" tool, rather than a "make nice documentation or professional reports" tool. 就像我之前说的,它更多的是一种“快速报告,以向主管显示数字和图表”,而不是“制作精美的文档或专业报告”工具。 Instead, I would invest in a nice automated latex workflow, and have a look at code formatting tools for displaying code, and simply wrap that code in a script that produces output to a file that you can then import into latex. 相反,我将投资于一个不错的自动化乳胶工作流程,并研究用于显示代码的代码格式化工具,并将该代码包装在一个脚本中,该脚本将产生的输出内容输出到一个文件中,然后可以将其导入到乳胶中。 It may sound like more work, but it's a lot more flexible and robust in the long term, particularly since the formatting commands can be very quirky as well as limited. 这听起来可能需要更多工作,但从长远来看,它会更加灵活和健壮,尤其是因为格式化命令既非常古怪又有限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM