MATLAB: set current folder to script locaton

I have several scripts and data in different folders, and I often use addpath and relative paths. My problem is that this only works if my current folder is where the script that I am executing is located. For example, if I execute script A, which adds the path X, and then runs script B, which lies in the path X, Matlab does not automatically change the folder, and the relative paths specified in script B no longer work.

Is there a way to automatically set the current folder to the script folder that I'm executing?

/ edit: It should be noted that I use these scripts on different computers with different drive names, so using absolute paths will probably not help.

+6
source share
1 answer

Put the following line in the script, it will set the current directory = script directory

 cd(fileparts(mfilename('fullpath'))) 
+9
source

All Articles