如何在 Ocatve 中制作具有多个功能的功能文件 [英] How to make a function file in Ocatve with multiple functions

查看:56
本文介绍了如何在 Ocatve 中制作具有多个功能的功能文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以在 Octave 中创建一个函数文件,其中文件名与定义一个函数的函数相同,但我想在一个文件中定义多个函数.有什么办法可以做到这一点,或者我是否需要为每个函数创建一个单独的文件.

I know that you can make a function file in Octave in which the file name is the same as the function which defines one function, but I would like to define multiple functions in one file. Is there any way to do this, or do I need a separate file for each function.

推荐答案

在这个答案中,我将假设您的主要目标是一个整洁的工作区,而不是明确的单一文件要求.

In this answer I will assume that your main objective is a tidy workspace rather than explicitly a one-file requirement.

让我们摆脱单一文件的方法.您可以创建一个 script m-file(不是函数 m-file),并定义多个 命令行函数.Octave 手册有一节介绍了这一点.举个例子:

Let's get the one-file approach out of the way. You can create a script m-file (not a function m-file), and define a number of command-line functions there. The octave manual has a section on this. Here's an example:

% in file loadfunctionDefinitions.m
1;   % statement with side-effect, to mark this file as a script. See docs.
function Out = Return1(); Out = 1; end
function Out = Return2(); Out = 2; end
% ... etc

% in your main octave session / main script:
X = Return1() + Return2();

但是,通常不建议这样做.特别是如果您需要与 matlab 兼容的代码,因为 matlab 引入脚本本地函数" 比八度晚得多,并决定以与现有八度实现不兼容的方式进行:matlab 期望脚本本地要在脚本末尾定义的函数;Octave 期望在第一次使用之前定义它们.但是,如果您使用普通的函数文件,则一切正常.

However, this is generally not recommended. Especially if you would require matlab compatible code, since matlab introduced 'script-local functions' much later than octave, and decided to do it in a manner incompatible to the existing octave implementation: matlab expects script-local functions to be defined at the end of the script; octave expects them to be defined before first use. However, if you use normal function files, everything is fine.

虽然我很欣赏我不喜欢充满功能的文件夹"情感,每个文件一个功能的方法实际上有很多好处(特别是如果你从终端编程,这使得丰富的工具有用两倍).例如.您可以轻松地使用 grep 来查找哪些函数使用了特定变量.或者比较来自不同提交等的各个函数的变化.

While I appreciate the "I don't like a folder full of functions" sentiment, the one-function-per-file approach actually has a lot of benefits (especially if you program from the terminal, which makes a wealth of tools twice as useful). E.g. you can easily use grep to find which functions make use of a particular variable. Or compare changes in individual functions from different commits, etc.

通常的问题更多是当存在其他重要文件时,例如,将此类函数文件乱扔到目录中.数据等,并且在一个地方拥有如此多的文件会使您难以找到想要的内容,并且感觉不整洁.但是,除了具有命令行定义的单个文件之外,您还可以采用许多其他方法,从编程的角度来看,这些方法可能也更好,例如:

Typically the problem is more one of having such function files littering the directory, when other important files are present, e.g. data etc, and having so many files in one place makes finding what you want hard to spot, and feels untidy. But rather than have a single file with command-line definitions, there are a number of other approaches you can take, which are probably also better from a programmatic point of view, e.g.:

  • 只需创建一个辅助函数"文件夹,然后将其添加到您的路径中即可.
  • 在适当的时候在主函数中使用子函数,以尽量减少不必要的文件数量
  • 使用私有函数文件夹
  • 使用'包目录',即一个以字符+"开头的文件夹,它为里面包含的函数创建了一个命名空间.例如.~/+MyFunctions/myfun.m 将通过 MyFunctions.myfun()~/ 访问,而无需添加 +MyFunctions 到路径(实际上您不应该这样做).
  • 创建一个合适的类目录,并使您的函数成为该类的方法
  • Simply create a 'helper functions' folder, and add it to your path.
  • Use subfunctions in your main functions whenever this is appropriate, to minimize the number of unnecessary files
  • Use a private functions folder
  • Use a 'package directory', i.e. a folder starting with the character '+', which creates a namespace for the functions contained inside. E.g. ~/+MyFunctions/myfun.m would be accessed from ~/ via MyFunctions.myfun(), without having to add +MyFunctions to the path (in fact you're not supposed to).
  • Create a proper class directory, and make your functions methods of that class

最后一个选项也可以实现单文件解决方案,如果您使用更新样式的基于 classdef 的类,它允许您在与类定义相同的文件中定义方法.但是请注意,对 classdef 定义的类的八度支持是 仍然有些限制.

The last option may also achieve a one-file solution, if you use a newer-style classdef based class, which allows you to define methods in the same file as the class definition. Note however that octave-support for classdef-defined classes is still somewhat limited.

这篇关于如何在 Ocatve 中制作具有多个功能的功能文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆