MATLAB与名称空间最接近的东西是什么? [英] What is the closest thing MATLAB has to namespaces?

查看:73
本文介绍了MATLAB与名称空间最接近的东西是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的实验室中有很多MATLAB代码.问题在于,实际上没有办法对其进行组织.由于所有函数都必须位于同一文件夹中才能被调用(或者您必须在MATLAB的path环境变量中添加一堆文件夹),所以看来我们注定要在同一文件夹中加载大量文件,在全局名称空间中.有没有更好的方法来组织我们的文件和功能?我真的希望有某种模块系统...

We have a lot of MATLAB code in my lab. The problem is there's really no way to organize it. Since all the functions have to be in the same folder to be called (or you have to add a bunch of folders to MATLAB's path environment variable), it seems that we're doomed have loads of files in the same folder, all in the global namespace. Is there a better way to organize our files and functions? I really wish there were some sort of module system...

推荐答案

MATLAB有一个可嵌套的包概念,其中包括类和函数.

MATLAB has a notion of packages which can be nested and include both classes and functions.

只需在路径上的某个位置以+作为第一个字符的目录即可,例如+mypkg.然后,如果该目录中存在类或函数,则可以将其称为mypkg.mything.您也可以使用import mypkg.mysubpkg.*从包中导入.

Just make a directory somewhere on your path with a + as the first character, like +mypkg. Then, if there is a class or function in that directory, it may be referred to as mypkg.mything. You can also import from a package using import mypkg.mysubpkg.*.

关于将一堆函数移入程序包的一个主要难题是,函数和类不会自动导入它们所驻留的程序包.这意味着,如果在相互调用的不同m文件中有一堆函数,则可能需要花费一些时间来插入import或限定函数调用.别忘了将导入放入也可以调出的子函数中.更多信息:

The one main gotcha about moving a bunch of functions into a package is that functions and classes do not automatically import the package they live in. This means that if you have a bunch of functions in different m-files that call each other, you may have to spend a while dropping imports in or qualifying function calls. Don't forget to put imports into subfunctions that call out as well. More info:

http://www.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html

这篇关于MATLAB与名称空间最接近的东西是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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