是否可以在 MATLAB 中为每个文件定义多个函数,并从该文件外部访问它们? [英] Is it possible to define more than one function per file in MATLAB, and access them from outside that file?

查看:20
本文介绍了是否可以在 MATLAB 中为每个文件定义多个函数,并从该文件外部访问它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我攻读 EE 本科学位时,MATLAB 要求在自己的文件中定义每个函数,即使是单行函数.

When I was studying for my undergraduate degree in EE, MATLAB required each function to be defined in its own file, even if it was a one-liner.

我现在正在攻读研究生学位,我必须在 MATLAB 中编写一个项目.这是否仍然是较新版本的 MATLAB 的要求?

I'm studying for a graduate degree now, and I have to write a project in MATLAB. Is this still a requirement for newer versions of MATLAB?

如果可以在一个文件中放置多个函数,是否有任何限制?例如,文件中的所有函数都可以从文件外部访问,还是只能访问与文件同名的函数?

If it is possible to put more than one function in a file, are there any restrictions to this? For instance, can all the functions in the file be accessed from outside the file, or only the function that has the same name as the file?

注意:我使用的是 MATLAB 版本 R2007b.

Note: I am using MATLAB release R2007b.

推荐答案

m 文件中的第一个函数(即 main 函数),在调用该 m 文件时调用.不要求主函数与m文件同名,但为了清楚起见,它应该.当函数名和文件名不同时,必须使用文件名来调用主函数.

The first function in an m-file (i.e. the main function), is invoked when that m-file is called. It is not required that the main function have the same name as the m-file, but for clarity it should. When the function and file name differ, the file name must be used to call the main function.

m 文件中的所有后续函数,称为 局部函数(或旧术语中的子函数"),只能由该 m 文件中的主函数和其他局部函数调用.其他 m 文件中的函数不能调用它们.从 R2016b 开始,您可以向脚本添加本地函数 同样,尽管作用域行为仍然相同(即它们只能从脚本内部调用).

All subsequent functions in the m-file, called local functions (or "subfunctions" in the older terminology), can only be called by the main function and other local functions in that m-file. Functions in other m-files can not call them. Starting in R2016b, you can add local functions to scripts as well, although the scoping behavior is still the same (i.e. they can only be called from within the script).

此外,您还可以在其他函数中声明函数.这些被称为嵌套函数,并且只能从在函数中,它们是嵌套的.它们还可以访问嵌套它们的函数中的变量,这使得它们非常有用,尽管使用起来有点棘手.

In addition, you can also declare functions within other functions. These are called nested functions, and these can only be called from within the function they are nested. They can also have access to variables in functions in which they are nested, which makes them quite useful albeit slightly tricky to work with.

更多的思考...

有一些方法可以绕过上面概述的正常函数范围行为,例如传递 函数句柄作为输出参数,如SCFrenchJonas(从 R2013b 开始,由 localfunctions 函数促进).但是,我不建议养成使用此类技巧的习惯,因为可能有更好的选择来组织您的功能和文件.

There are some ways around the normal function scoping behavior outlined above, such as passing function handles as output arguments as mentioned in the answers from SCFrench and Jonas (which, starting in R2013b, is facilitated by the localfunctions function). However, I wouldn't suggest making it a habit of resorting to such tricks, as there are likely much better options for organizing your functions and files.

例如,假设您在 m 文件 Am 中有一个主函数 A,以及局部函数 DEF.现在假设您在 m 文件 BmCm 中分别有另外两个相关的函数 BC,您还希望能够调用 DEF.以下是您的一些选择:

For example, let's say you have a main function A in an m-file A.m, along with local functions D, E, and F. Now let's say you have two other related functions B and C in m-files B.m and C.m, respectively, that you also want to be able to call D, E, and F. Here are some options you have:

  • DEF 分别放在各自独立的 m 文件中,允许任何其他函数调用它们.缺点是这些函数的范围很大,不仅限于ABC,但优点是这很简单.

  • Put D, E, and F each in their own separate m-files, allowing any other function to call them. The downside is that the scope of these functions is large and isn't restricted to just A, B, and C, but the upside is that this is quite simple.

使用 DEF 创建一个 defineMyFunctions m 文件(如 Jonas 的示例) 作为本地函数和一个简单地返回函数句柄的主函数.这允许您将 DEF 保存在同一个文件中,但它不会对这些函数的范围做任何事情因为任何可以调用 defineMyFunctions 的函数都可以调用它们.然后,您还必须担心将函数句柄作为参数传递,以确保将它们放在需要的地方.

Create a defineMyFunctions m-file (like in Jonas' example) with D, E, and F as local functions and a main function that simply returns function handles to them. This allows you to keep D, E, and F in the same file, but it doesn't do anything regarding the scope of these functions since any function that can call defineMyFunctions can invoke them. You also then have to worry about passing the function handles around as arguments to make sure you have them where you need them.

DEF复制到BmCm中> 作为本地函数.这将它们的使用范围限制在 ABC,但使代码的更新和维护成为一场噩梦,因为您有相同代码在不同地方的三个副本.

Copy D, E and F into B.m and C.m as local functions. This limits the scope of their usage to just A, B, and C, but makes updating and maintenance of your code a nightmare because you have three copies of the same code in different places.

使用私有函数 如果您在同一目录中有 ABC,则可以创建一个名为 private 并将 DEF 放在那里,每个都作为单独的 m 文件.这限制了它们的作用域,因此它们只能被上面目录中的函数(即 ABC)调用并保留它们一起在同一个地方(但仍然不同的 m 文件):

Use private functions! If you have A, B, and C in the same directory, you can create a subdirectory called private and place D, E, and F in there, each as a separate m-file. This limits their scope so they can only be called by functions in the directory immediately above (i.e. A, B, and C) and keeps them together in the same place (but still different m-files):

myDirectory/
    A.m
    B.m
    C.m
    private/
        D.m
        E.m
        F.m

所有这些都超出了您的问题范围,并且可能比您需要的更详细,但我认为最好谈谈组织所有 m 文件的更普遍的关注.;)

All this goes somewhat outside the scope of your question, and is probably more detail than you need, but I thought it might be good to touch upon the more general concern of organizing all of your m-files. ;)

这篇关于是否可以在 MATLAB 中为每个文件定义多个函数,并从该文件外部访问它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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