How can I maintain multiple MATLAB path setups and switch between them easily?

5 vues (au cours des 30 derniers jours)
In my work I often need to switch between two "environments" which correspond to different MATLAB path settings. The path setups are mutually exclusive because the same function may be present in both environments. For example the directory structure is:
/release_folder
/myFunc.m
/development_folder
/myfunc.m
How can I switch between these environments quickly, easily, and without creating issues related to 'restoredefaultpath' ?
 

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 7 Juil 2017
Below is an example of how one might accomplish this. The key points of the approach are:
1. In order to "reset" the path before switching streams use 'pathdef' to load the path MATLAB started with instead of using 'restoredefaultpath'. This keeps support packages on the path.
2. Create a helper function for each stream which sets up the path correctly for that stream.
3. Keep the the helper functions on the path so that it is easy to switch streams.
Example Code:
startup.m - Put the helper functions on the path when starting MATLAB.
if ~isdeployed % deployed applications will not run the code below
% This folder contains useDevelopmentPath and useReleasePath
addpath('H:\Documents\MATLAB\pathtools');
end
pathtools\useDevelopmentPath.m - sets up path for development stream
function useDevelopmentPath
  path(pathdef);
addpath('H:\Documents\MATLAB\pathtools'); % For useDevelopmentPath and useReleasePath
addpath('H:\Documents\MATLAB\development_folder'); % add all folders needed for development stream
  ...
end
pathtools\useReleasePath.m - sets up path for release stream.
function useReleasePath
    path(pathdef);
    addpath('H:\Documents\MATLAB\pathtools'); % For useDevelopmentPath and useReleasePath
    addpath('H:\Documents\MATLAB\release_folder');% add all folders needed for release stream
    ...
end
Using these helper functions would look like:
>> useDevelopmentPath
>> myFunc
myFunc: Development Version
>> useReleasePath
>> myFunc
myFunc: Release version.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by