3

I am in the progress of developing a MATLAB program that is growing constantly. It started out as a collection of scripts but has gotten bigger and bigger. Right now everything is in one big folder but 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 having to copy the MATLAB files.

Is there an alternative to adding all directories to the searchpath? How can I keep the codebase of a bigger MATLAB project tidy?

Rodia
  • 1,407
  • 8
  • 22
  • 29
mwurth
  • 68
  • 3
  • 3
    The answer to this question might be of help: http://stackoverflow.com/questions/2748302/what-is-the-closest-thing-matlab-has-to-namespaces – magnetometer Jan 09 '15 at 13:47

2 Answers2

1

Quick fix:

You could add all main programs in one root dir. Submodules are in folders below. At every main program you ensure 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);
BerndGit
  • 1,530
  • 3
  • 18
  • 47
0

I see there is already a link to something that resembles namespaces. However, if you don't want to go for packages, you could follow the structure that I currently use. I think this works especially well if you have a limited amount of big projects

Suppose you work on 2 projects, then make three parallel folders:

  1. Project 1
  2. Project 2
  3. General

Basically you can just start building Project 1 and Project 2 in their respective folder, and when you see similar operations being done in both of the projects (perhaps after a bit of generalization), you can move them to general. Just make sure general is lower on the search path, so that you will always find customized functions before generalized ones.

Of course, you can make subfolders in general as well.


Note that the easiest way to use this, would be to add the general folder to your path first, and afterwards the project folder. In that case you may still have a lot of files on the path, but this way there is no duplication, and you can easily see which critical files are relevant for a project.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122