How to use different path in different folder
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
gao shengyu
el 9 de Mayo de 2019
Respondida: Walter Roberson
el 9 de Mayo de 2019
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?
I found this answer How can I run scripts with the same name, that are contained in different folders?
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 comentarios
Respuesta aceptada
Stephane Dauvillier
el 9 de Mayo de 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 comentarios
Más respuestas (1)
Walter Roberson
el 9 de Mayo de 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 comentarios
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!