How can one switch between locations of same namespace in one file system?

Kind reader,
I work with a namespace to structure function families. The development folder is a git checkout and I managed to write an installer for the namespace. So the relesed state of the namespace is in the Program Data folder and the path is added in Matlab.
Questions: As boths namespace folders path are now added to Matlab
  1. How can I choose between the two?
  2. How can I activated/deactivate one of them?
---------------------- File system depiction --------------------------------
- C:gitClone/+myNamespace -C:ProgramData/myNamespace
- /+awsomeFunctions - /+awsomeFunctions
- /+helpfulFunctions - /+helpfulFunctions
-----------------------------------------------------------------------------
As I provide a release of the namespace to co-workers I have to be able to check the installation on my development computer. I read about package creation in Matlab (mpmcreate - Create package - MATLAB) but that does not work for namespaces.
Every hint, comment, and thought about this issue is highly appreciated :)
Environement: Windows with Matlab 2024b

 Accepted Answer

If you have two folders on the path that both include the same namespace folders, their contents will be merged. If there are files in those folders with the same name (and namespace), the ones that were added to the path second will shadow the other versions.
Given that both folders are clones of each other (one is the development version and the other is the production version), I assume they will have almost the same files, in which case, whichever version was added to the path second will "win" and the other one will be ignored.
For development purposes, I suggest writing a script to switch between the two different versions by adding and removing one or the other from the path. You can use which to find which version is "active" or which -all to find all versions of your scripts that are available.

6 Comments

If your goal is testing, and you are using the MATLAB Unit Test framework, you can use the matlab.unittest.fixtures.PathFixture to add a folder to the path temporarily for testing purposes.
Here is an example of such a script for switching between the two versions. Note, I wrote this without any testing (I didn't even run it), so you will probably need to make some tweaks to get it working.
function switchVersions(devOrProd)
arguments
devOrProd (1,1) string {mustBeMember(devOrProd,["dev","prod","toggle"])} = "toggle"
end
devPath = "C:\gitClone\";
prodPath = "C:\ProgramData\";
current = currentlyActive(devPath, prodPath);
if current ~= devOrProd
% Remove current
if current == "dev"
rmpath(devPath)
if devOrProd == "toggle"
devOrProd == "prod";
end
elseif current == "prod"
rmpath(prodPath)
if devOrProd == "toggle"
devOrProd == "dev";
end
elseif current == "neither" && devOrProd == "toggle"
% Need to pick one, could be either dev or prod.
devOrProd = "prod";
end
% Add new
if devOrProd == "dev"
addpath(devPath)
else
addpath(prodPath)
end
end
end
function devOrProd = currentlyActive(devPath, prodPath)
w = which('-all',<nameoffunctioninpackage>);
devInd = find(startsWith(w, devPath),1,'first');
prodInd = find(startsWith(w, prodPath),1,'first');
if isempty(devInd) && isempty(prodInd)
devOrProd = "neither";
elseif isempty(devInd)
devOrProd = "prod";
elseif isempty(prodInd)
devOrProd = "dev";
elseif devInd < prodInd
devOrProd = "dev";
else
devOrProd = "prod";
end
end
thanks a lot :) this is the push in the right direction I was hoping for. I realized to use which I need a file as it cannot find the folders. But I think that is doable.
I ran into problems with the function which. From what I understood the following code should give precedance in the search path to the production folder. But no matter in which order the addpatch commands are executed the development path is always the first entry when calling which. Any further hint on what to read next?
rmpath(devPath)
rmpath(prodPath)
addpath(prodPath)
addpath(devPath)
->
C:\svn\hilsystems\trunk\hilsystemslibroot.m
C:\ProgramData\\hilsystemslib\hilsystemslibroot.m % Shadowed
Whatever path is added second will get precedance... so in your code above, you called addpath(devPath) second, so I would expect the "C:\svn\" path would get priority, which is what I'm seeing. If you swapped the order, I would expect the prodPath would take over for the devPath.
I did some quick testing: I created two folders called "Test1" and "Test2". Both had a function named "foobar.m". I ran the following:
addpath('Test1')
foobar % Test 1 version ran
addpath('Test2')
foobar % Test 2 version ran
addpath('Test1')
foobar % Test 1 version ran
addpath('Test2')
foobar % Test 2 version ran
This supports what I thought to be the expected behavior.
If you aren't seeing that, then you may need to run rehash to tell MATLAB to recognize the change in path. I'm not sure the exact criteria under which you would need to run rehash, but give that a shot and see if it helps.
Another thing to be wary of: If you use something like mlock or persistent, or if you have an instance of a class stored in a variable in your workspace, that can cause MATLAB to continue using the old definition even after you've switched the path.
Thanks :)
I was not aware of the rehash function and it did the trick.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2024b

Asked:

on 12 Jun 2025

Commented:

on 16 Jun 2025

Community Treasure Hunt

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

Start Hunting!