如何在MATLAB中模拟“包含"行为? [英] How do I emulate 'include' behaviour in MATLAB?

查看:78
本文介绍了如何在MATLAB中模拟“包含"行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB中,我可以在一个文件中定义多个函数,只有第一个定义的函数在该文件外部可见.另外,我可以将每个函数放在自己的文件中,并通过路径在全局范围内使它们全部可见.我正在编写一个菜单驱动的应用程序,其中每个菜单项都运行一个不同的功能.当前,所有这些都在一个大文件中,这变得越来越难以导航.我想做的是将相关功能组放入单独的文件中.

In MATLAB I can define multiple functions in one file, with only the first defined function being visible external to that file. Alternatively, I can put each function in its own file and make them all globally visible through the path. I'm writing a menu driven application, where each menu item runs a different function. Currently, these are all in one big file, which is getting increasingly difficult to navigate. What I'd like to do is put groups of related functions into separate files.

我认为我可以通过将所有子功能放在单独的目录中,然后将该目录添加到父功能的路径中来执行此操作,但这感觉有点混乱和不雅.

I think I can do something like this by putting all the child functions into a separate directory and then adding the directory to the path in my parent function, but this feels a bit messy and inelegant.

谁能提出更好的建议?

注意:我对MATLAB 2006最熟悉,但是我正在升级到MATLAB 2009.

推荐答案

一个建议,它可以避免修改

One suggestion, which would avoid having to modify the MATLAB path, is to use a private function directory. For example:

假设您在目录\MATLAB\temp\中已经有一个名为test.m的函数(该路径已经在MATLAB路径中).如果test.m中有要放置在它们自己的m文件中的局部函数,并且只希望test.m可以访问它们,则首先应在\MATLAB\temp\中创建一个名为private的子目录.然后,将test.m中的各个本地函数m文件放在此private子目录中.

Let's say you have a function called test.m in the directory \MATLAB\temp\ (which is already on the MATLAB path). If there are local functions in test.m that you want to place in their own m-files, and you only want test.m to have access to them, you would first create a subdirectory in \MATLAB\temp\ called private. Then, put the individual local function m-files from test.m in this private subdirectory.

不需要将private子目录添加到MATLAB路径中(实际上,不应将 添加到路径中以使事情正常运行).只有在private子目录上方的目录中的文件test.m和其他m文件可以访问其包含的功能.使用私有函数,您可以有效地模拟局部函数(例如,作用域有限,函数重载等),而不必将所有函数都放在同一个m文件中(对于某些应用程序可能会变得很大).

The private subdirectory doesn't need to be added to the MATLAB path (in fact, it shouldn't be added to the path for things to work properly). Only the file test.m and other m-files in the directory immediately above the private subdirectory have access to the functions it contains. Using private functions, you can effectively emulate the behavior of local functions (i.e. limited scope, function overloading, etc.) without having to put all the functions in the same m-file (which can get very big for some applications).

这篇关于如何在MATLAB中模拟“包含"行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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