Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the closest thing MATLAB has to namespaces?

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

like image 352
rlbond Avatar asked Apr 30 '10 23:04

rlbond


People also ask

Does MATLAB have namespaces?

The names of classes and functions are scoped to the package folder. A package is a namespace within which names must be unique. Function and class names must be unique only within the package. Using a package provides a means to organize classes and functions.

What is MATLAB package?

Package MATLAB® functions for use in applications coded in other languages. Before you can integrate MATLAB functions into external applications, you need to package them for the target language. MATLAB Compiler SDK™ includes two apps and a command-line compiler for this purpose.


2 Answers

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

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

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

like image 78
kwatford Avatar answered Oct 12 '22 12:10

kwatford


I don't see the problem with having to add some folder to Matlab's search path. I have modified startup.m so that it recursively looks for directories in my Matlab startup directory, and adds them to the path (it also runs svn update on everything). This way, if I change the directory structure, Matlab is still going to see all the functions the next time I start it.

Otherwise, you can look into object-oriented code, where you store all the methods in a @objectName folder. However, this may lead to a lot of re-writing code that can be avoided by updating the path (there is even a button add with subfolders if you add the folder to the path from the File menu) and doing a bit of moving code.

EDIT

If you would like to organize your code so that some functions are only visible to the functions that call them directly (and if you don't want to re-write in OOP), you put the calling functions in a directory, and within this directory, you create a subdirectory called private. The functions in there will only be visible to the functions in the parent directory. This is very useful if you have to overload some built-in Matlab functions for a subset of your code.

like image 33
Jonas Avatar answered Oct 12 '22 10:10

Jonas