5

I'm working on a project containing some subprojects. Each subproject is located in a own folder.

projDir/subProj1
       /subProj2

and so on. Each subproject is a standalone running project. But now I want to use some functions of i.e. subProj1 in subProj2. But the functions in subProj1 should not be visible in general. So it is no good idea, to add the subProj1-path to the MATLAB-Path generally. Hence, I want to add this path in my .m-file stored in subProj2 and after finishing this script, the path should be removed (automatically) by it's own. Is there any possibility, to add a path temporarily to the MATLAB-path variable?

paul_schaefer
  • 397
  • 4
  • 16

2 Answers2

5

The addpath function only adds the files/folder to your path for the current Matlab session, assuming you don't call savepath. You also might find the genpath function helpful if you want to add subfolders.

Dan
  • 45,079
  • 17
  • 88
  • 157
  • aha, ok thank you. I thought `addpath` will add the path permanently. I will try it... – paul_schaefer Mar 10 '15 at 10:21
  • @paul_schaefer I thought exactly the same thing. I think I even wrote to the Mathworks to ask them to clarify this in the documentation. Maybe you can do the same? – Dan Mar 10 '15 at 10:22
  • ok, that works. Thanks a lot. Interesting, because in the documentation they explicitly point, that a call of `addpath` within a function persists, even if the function is leaved. On the other hand, they suggest to use this in a startup script to modify the path after starting MATLAB.... – paul_schaefer Mar 10 '15 at 10:25
  • 1
    However changes made to the path, even if not saved, may effect the execution of other functions called later in the same session (as pointed out in documentation)-which may not be desireable. Changes to the path that only have within-function scope would be nice, without the crud of creating termporary local pathdef files. – Jacob Lee Feb 28 '19 at 17:37
1

You can use path(path_to_add,path) to add a path to the current path variable. Unless you do savepath you won't affect the global path.

I would do path(strcat(pwd,'\subProj1',path) etc. in the config .m script you have.

tuna_fish
  • 390
  • 2
  • 11