Adding a pathdef.m file, as a string, to the current path

2 views (last 30 days)
Charles Pace
Charles Pace on 5 Mar 2021
Commented: Rik on 5 Mar 2021
Hello,
I have saved a pathdef file ('pathdef_etc.m') which contains the paths that I need for a given project. I simply want to get the string output from this pathdef file, so that I can add it to my current path using 'addpath( path_string )'.
How can I get this pathdef file as a string?
Thank you
  2 Comments
Walter Roberson
Walter Roberson on 5 Mar 2021
What is the file format? Did you create it with savepath ?
Are the individual paths separated by colon or by newlines?
Is this for Windows or (not Windows) ?

Sign in to comment.

Accepted Answer

Jorg Woehl
Jorg Woehl on 5 Mar 2021
You probably want to use textscan for this:
fileID = fopen('pathdef_etc.m')
C = textscan(fileID, '%s', 'Delimiter', '\n')
fclose(fileID)
The output is a single cell containing a n-by-1 cell array of character vectors (one per line in the input file). So C{1}{1} contains the first line of your file, C{1}{2} the second, and so on. You could then set up a loop to add each line to the path:
for i=1:size(C{1},1)
addpath(C{1}{i})
end
Hope this helps!
  3 Comments
Rik
Rik on 5 Mar 2021
That suggests you can use this as well:
addpath(pathdef);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!