Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: Set current folder to script's locaton

Tags:

path

matlab

I have a handful of scripts and data in different folders and I use addpath and relative paths very often. My problem is, this only works if my current folder is where the script that I execute is located. For example, if I execute script A which adds path X and later execute script B which lies in path X, Matlab doesn't automatically change the folder and relative paths specified in script B don't work anymore.

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

/edit: I should note that I use these scripts on different computers with different drive names, so using absolute paths probably won't help.

like image 441
Fred S Avatar asked Feb 27 '13 03:02

Fred S


People also ask

How do I put the file path in MATLAB?

Create the folder matlab/myfiles . Then, add it to the top of the search path, disable folder change notification, and return the search path before adding the folder. mkdir('matlab/myfiles') oldpath = addpath('matlab/myfiles','-frozen'); Disabling folder change notification is not supported in MATLAB® Online™.

What is the current directory folder that MATLAB is in?

Description. pwd returns the MATLAB® current folder. currentFolder = pwd returns the path to the current folder.


1 Answers

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

cd(fileparts(mfilename('fullpath')))
like image 101
zamazalotta Avatar answered Sep 28 '22 05:09

zamazalotta