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

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 commentaires

Charles Pace
Charles Pace le 5 Mar 2021
Modifié(e) : Charles Pace le 5 Mar 2021
I am looking for something like...
*from the directory containing the pathdef file*
display(pathdef.m),
so that I can type addpath(ans)
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) ?

Connectez-vous pour commenter.

 Réponse acceptée

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 commentaires

Hi, thank you for the answer! I will give this a try.
It is just that I think there is a really straightforward answer. I saw it implemented once, I just can't remember how. It was almost as simple as just clicking the pathdef file in the file explorer, then addpath(ans) or something like that.
When I click the pathdef.m file, it opens up a function p=pathdef , which includes the pathdef search results (all the folders in path), and in the header says..
% PATHDEF returns a string that can be used as input to MATLABPATH
% in order to set the path.
Which makes me think it should be simpler, without any parsing or looping.
However, your help/code has gotten me halfway there so thanks alot! This may work fine.
I just figured it out actually. It was really simple. I just needed to find the pathdef file in the explorer, right click -> 'run',
then the path appeared in command window as one really long string.. 'ans = path1, path2, etc .... '
then just addpath(ans).
Thank you for your help though.
That suggests you can use this as well:
addpath(pathdef);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by