What is the closest thing MATLAB has to namespaces?

后端 未结 4 1918
挽巷
挽巷 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:16

    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.

提交回复
热议问题