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.
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™.
Description. pwd returns the MATLAB® current folder. currentFolder = pwd returns the path to the current folder.
Put the following line in the script, it would set the current directory = script directory
cd(fileparts(mfilename('fullpath')))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With