Managing Larger MATLAB Projects

I am developing a MATLAB program which is constantly growing. It started out as a collection of scripts, but got bigger and bigger. Now everything is in one big folder, but in order to separate independent modules, I want to divide them into subfolders. There is some dependency between modules, so I want to be able to access functions from different modules without copying MATLAB files.

Is there an alternative to adding all directories to the search path? How can I save the codebase of a larger MATLAB project?

+3


source to share


2 answers


Quick fix:

You can add all major programs to one root directory. The submodules are in the folders below. In every main program, you make sure that all paths are set correctly. At the end of the program, you restore the original path settings



% Begin of main program. Set path to all subfolders
save_path = path;
curr_dir = strrep(which(mfilename('fullpath')),mfilename,'')
addpath(genpath(curr_dir))

% Main program 
....
....
....

% Restore original Path settings
path(save_path);

      

+1


source


I can see that there is already a reference to something similar to namespaces. However, if you don't want to go for packages, you can follow the structure I'm currently using. I think this works especially well if you have a limited number of large projects.

Let's say you are working on 2 projects, then create three parallel folders:

  • Project 1
  • Project 2
  • General

Basically, you can just start creating Project 1 and Project 2 in their respective folder, and when you see similar operations performed in both projects (perhaps after some generalization), you can move them to the shared one. Just make sure the generic path is lower in the search path so that you always find custom functions before generic ones.



You can of course also create subfolders in general.


Note that the easiest way to use this is to add the shared folder to your path first and then the project folder. In this case, you can have many files in the path, but this way there is no duplication and you can easily see which critical files are relevant to the project.

0


source







All Articles