Filter löschen
Filter löschen

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

13 Ansichten (letzte 30 Tage)
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\...')

Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 1 Feb. 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 Kommentare

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Austin M. Weber
Austin M. Weber am 1 Feb. 2024
Bearbeitet: Austin M. Weber am 1 Feb. 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 \.

Image Analyst
Image Analyst am 1 Feb. 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.

Kategorien

Mehr zu Search Path finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by