An automatic way to change \ with / (windows vs. unix) only for "addpath"?

53 vues (au cours des 30 derniers jours)
Sim
Sim le 1 Fév 2024
Commenté : Sim le 1 Fév 2024
Is there an automatic way to change \ with / (windows vs. unix) only for "addpath"
As example, I would need to convert many addpaths from unix
addpath('/Users/XXX/Documents/...')
to windows:
addpath('C:\Users\YYY\Desktop\...')

Réponse acceptée

Cris LaPierre
Cris LaPierre le 1 Fév 2024
use fullfile to build your path. It will use the separator appropriate for your OS.
"fullfile replaces all forward slashes (/) with backslashes (\) on Windows. On UNIX® platforms, the backlash (\) character is a valid character in file names and is not replaced"
  5 commentaires
Sim
Sim le 1 Fév 2024
+1 as well!

Connectez-vous pour commenter.

Plus de réponses (2)

Austin M. Weber
Austin M. Weber le 1 Fév 2024
Modifié(e) : Austin M. Weber le 1 Fév 2024
file_name = '/Users/XXX/Documents/...';
file_name = strrep(file_name,'/','\');
addpath(file_name)
Warning: Name is nonexistent or not a directory: /users/mss.system.A3HcnN/\Users\XXX\Documents\...
EDIT: Just to explain, I am using the strrep function (string replace) to replace any instances of / with \.
  1 commentaire
Sim
Sim le 1 Fév 2024
thanks a lot both @Austin M Weber and @Cris LaPierre!!! Very nice solutions! I would accept both solutions!!

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 1 Fév 2024
@Sim the above answers are correct but only partial -- they're incomplete. I understood that you need to do this for lots of files, not just one so those solutions need to be inserted into some code that will process all your m-files in the folder. If it was just a few files, you could just simply use the find/replace functionality to replace them all in the editor.
But since you probably have lots of files you probably want to process them all automatically. You can use the FAQ: Process a sequence of files
There is a code snippet there that you can modify so that it reads in m-files. Then, in the loop, for each file, you would call readlines, then loop over all cells calling strrep add the drive letter and (optionally) to replace the slashes. Something like
ca = readlines(thisFileName);
oldString = "addpath('/Users";
for kk = 1 : numel(ca)
% Add C:
ca{kk} = strrep(ca{kk}, oldString, "addpath('C:/Users");
end
Make sure you use double quotes around the string since there is a single quote in the string you want to replace.
Then call writelines to overwrite the file with the new lines.
writelines(ca, thisFileName);
MATLAB under Windows understands forward slashes (like unix uses) perfectly fine, so there is no need to switch the direction of the slashes. But you will need to add the drive letter, so you might as well do both. Once you add the drive letter I don't think it will work anymore in unix anyway so I don't think you can have one file work in both. If you wanted to do it dynamically, if you can find some way to detect the operating system then you could add or remove the drive letter in the loop depending on what the operating system is. Then you could use fullfile, or just simply leave them all as forward slashes, which Windows has no problem with.
If this helps, please vote for my answer.
  2 commentaires
Cris LaPierre
Cris LaPierre le 1 Fév 2024
Note that the user account in the example also changes from 'XXX' to 'YYY"
Sim
Sim le 1 Fév 2024
Thanks a lot @Image Analyst!!! :-) :-)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Solar Power dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by