An automatic way to change \ with / (windows vs. unix) only for "addpath"?
54 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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\...')
0 Kommentare
Akzeptierte Antwort
Cris LaPierre
am 1 Feb. 2024
"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
Weitere Antworten (2)
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)
EDIT: Just to explain, I am using the strrep function (string replace) to replace any instances of / with \.
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.
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 Kommentare
Cris LaPierre
am 1 Feb. 2024
Note that the user account in the example also changes from 'XXX' to 'YYY"
Siehe auch
Kategorien
Mehr zu Solar Power 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!