How can I create paths relative to a specific folder to facilitate moving project folders to other locations?

17 views (last 30 days)
Hello everyone!
I would like to find a better way to use paths in Matlab. I often use Matlab scripts to postprocess data, for example by going through many text files and reading their content into Matlab for further analysis. The "problem" I have is that when I have my files in very nested locations and I move the folder containing the files and the script, I have to re-specified the whole path again in the script. I would like to specify the location of the txt files to read relative to the folder that contains the script. How can I achieve this?
Example of project folder:
C:\Users\myusername\Documents\subfolder1\subfolder2\subfolder3\myProject_Folder
'myProject_Folder' contains my txt files in a subfolder:
C:\Users\myusername\Documents\subfolder1\subfolder2\subfolder3\myProject_Folder\txt_files_Folder
and a script, for example
script.m
In script.m I have these lines for example:
outputDir = 'C:\Users\myusername\Documents\subfolder1\subfolder2\subfolder3\myProject_Folder'; %same as Project Folder
txt_files_path = ' C:\Users\myusername\Documents\subfolder1\subfolder2\subfolder3\myProject_Folder\txt_files_Folder'
I would like to have \myProject_Folder as the root folder, so that when I move myProject_Folder and its subfolders to another computer I do not have reenter all the paths again in script.m.
Thanks in advance!
Best regards,
Adriel Perez

Accepted Answer

David Goodmanson
David Goodmanson on 28 Aug 2018
Hi Adriel,
Here's one approach, concatenation
% give the path a short name, the idea is on the computer you only have to specify this once
outD = 'C:\Users\myusername\Documents\subfolder1\subfolder2\subfolder3\myProject_Folder';
% then
txt_files_path = [outD, '\txt_files_Folder']
txt_files_path2 = [outD, '\new_txt_files_Folder2']
% and so forth.
You can also make use of the cd function. In my case
Cpath = cd
Cpath = 'c:\users\dave\tech\matlab'
which is a string you can concatenate with something else as with outD, or lop part of it off.
If you have a home base and want to go to the end of some path and temporarily make it the local directory you can do
home_path = cd % save your location
% and
cd(txt_files_path)
Then you can read files in and out locally with no path prefix. After that, cd(home_path) takes you back from whence you came.
Lots of possibilities, this is kind of the 'original' way and if there are more sophisticated ways I imagine others will weigh in.
  4 Comments
Adriel Perez Tellez
Adriel Perez Tellez on 28 Aug 2018
Edited: Adriel Perez Tellez on 28 Aug 2018
Thanks everyone!
@ David Goodmanson Thanks! I still need to specify the full path at least once, as you did with
outD
Ideally I would need a line of code in my script that would retrieve the full path to the script. I did not know about cd, thanks!
Adriel Perez Tellez
Adriel Perez Tellez on 28 Aug 2018
Edited: Adriel Perez Tellez on 28 Aug 2018
Hi again! Just wanted to share this. This code:
[outD, ~ , ~] = fileparts(which(mfilename))
gives me the folder containing my script

Sign in to comment.

More Answers (2)

Adriel Perez Tellez
Adriel Perez Tellez on 28 Aug 2018
I will be using
fullfile
from now on, thanks!

Sean de Wolski
Sean de Wolski on 28 Aug 2018
mfilename('fullpath'), fullfile, fileparts, pwd are my best friends for this type of work.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!