Search code examples
matlabmatlab-deployment

Managing bigger MATLAB projects


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?


Solution

  • 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);