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

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

问题描述



当我在EE学习本科学位课程时,MATLAB需要在自己的文件中定义每个函数。我正在读研究生学位,我必须在MATLAB中编写一个项目。这是否仍然是较新版本的MATLAB的要求?



如果可以在文件中放入多个函数,是否有限制?例如,文件中的所有功能是否可以从文件外部访问,或者仅与文件名称相同的功能?



注意:我正在使用MATLAB版本R2007b。

解决方案

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



m文件中的所有后续功能,称为本地功能(或旧术语中的子功能 ),只能通过主函数和该m文件中的其他本地函数调用。其他m文件中的函数不能调用它们。



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



更多的思想食物。 ..



有关上述正常功能范围界定行为的方法,例如传递函数句柄作为输出参数,如 Jonas的答案。但是,我不会建议使用这种技巧的习惯,因为组织您的文件可能会有更好的选择。



例如,让我们说你在m文件 Am 中具有主要功能 A ,以及本地功能 D E F 。现在我们假设你有两个其他相关的功能 B C 在m文件 Bm Cm ,您也希望能够调用 D E F 。以下是您拥有的一些选项:




  • D E F 各自在其单独的m文件中,允许任何其他功能调用它们。缺点是这些功能的范围很大,不仅限于 A B C ,但这是很简单的。


  • 创建一个在$ code> D E 中定义MyFunctions m-file(像Jonas的例子)作为本地函数的 F 和一个简单地向其返回函数句柄的主函数。这允许您保持 D E F 在同一个文件中,但是它不会对这些函数的范围做任何事情,因为可以调用 defineMyFunctions 的函数可以调用它们。您还需要担心将函数句柄作为参数传递,以确保您需要它们。


  • 复制 D E F Bm Cm 作为本地功能。这将其使用范围限制在 A B C ,但是您的代码更新和维护是一场噩梦,因为您在不同的地方有三个相同代码的副本。


  • 使用私人功能如果您有<$ c同一目录中的$ c> A , B C ,您可以创建一个名为 private 并放置 D E 的子目录,以及在那里,每个作为一个单独的m文件 F 这限制了它们的范围,因此它们只能由紧接在上面的目录中的函数(即 A B

     

    myDirectory /
    Am
    Bm
    Cm
    private /
    Dm
    Em
    Fm




所有这些都超出了您的问题的范围,可能更多细节比你需要的更多,但是我觉得可以很好的了解组织所有m文件的一般情况。 ;)


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.

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?

Note: I am using MATLAB release R2007b.

解决方案

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.

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.

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.

More food for thought...

There are ways around the normal function scoping behavior outlined above, such as passing function handles as output arguments as mentioned in Jonas' answer. However, I wouldn't suggest making it a habit of resorting to such tricks, as there are likely much better options for organizing your files.

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:

  • 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.

  • 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.

  • 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.

  • 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
    

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天全站免登陆