What is the closest thing MATLAB has to namespaces?

后端 未结 4 1931
挽巷
挽巷 2020-11-28 23:55

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

4条回答
  •  难免孤独
    2020-11-29 00:14

    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

提交回复
热议问题