MATLAB Answers

How to use different path in different folder

4 views (last 30 days)
As all known, when we set MATLAB search path, we can use the function in that search path.
However if we have different projects, and their functions are same name, but these functions are slightly different. Eg.
C:\project1\myadd.m
C:\project1\mymul.m
C:\porject1\myconv.m
C:\project1\detector\svm.m
C:\project2\myadd.m
C:\project2\mymul.m
C:\porject2\myconv.m
C:\project2\detector\svm.m
When we work in project1 folder, we can add project1 to search path. When we work in project2 folder, should we remove project1 from search path and add project2 to path?
It seems to solve my question, but when the project is very large, it seems don't work, espically I clone other's code.
So, I just want to using corresponding search path when I work in different folders easily .
I tried to save search path as `pathdef.m` to working folder, but it doesn't work.

  0 Comments

Sign in to comment.

Accepted Answer

Stephane Dauvillier
Stephane Dauvillier on 9 May 2019
Ok there is several possibility.
Option1a: You want your function foo to be used ONLY by the functions of your project1, then you can make it private function. By creating a folder named private, all the function inside that folder can only be called by the function in that folder and the parent one. Plus if yoyur current folder is the private one, you can call the function. TYhe private folder cannot be added to the MATLAB path.
Option1b: Same thing as option1 but either you have MATLAB R2019a or you are in a simulink projet. By creating a project you will be able to have a specific path for that project. This path will be added whenever you open the project and remove when you close the project.
Option2a: You really want to use the function foo define two different folder depending on the case. Then you can create class. Because the methods have higher proprity than fucntion, if you have two class name CLASS1 and CLASS2 and both of them have a method plot, then plot(obj) will call the method plot of CLASS1 if obj is an instance of CLASS1
So if you have
classdef CLASS1
methods(public)
function obj = CLASS1()
...
end
function plot(obj)
figure
title('This is CLASS1')
end
end
end
and
classdef CLASS2
methods(public)
function obj = CLASS2()
...
end
function plot(obj)
figure
title('This is CLASS2')
end
end
end
then this scriopt will dowhat it is supposed to do
obj1=CLASS1();
obj2=CLASS2();
plot(obj1)
plot(obj2)
Option2b: yiou can create package so if you define a function foo in folder named +package1 and +package2 and these folder are in the MATLAB path, this is how you can use specific location function
package1.foo(var)
package2.foo(var)

  0 Comments

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 9 May 2019
Use /private folders. Those can only be used by routines in closely related directories.
Or perhaps you should be creating classes that happen to provide a number of similar method names.
Or perhaps you should use packages and import()

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R2018b