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

23 visualizaciones (últimos 30 días)
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\...')

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 1 de Feb. de 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 comentarios

Iniciar sesión para comentar.

Más respuestas (2)

Austin M. Weber
Austin M. Weber el 1 de Feb. de 2024
Editada: Austin M. Weber el 1 de Feb. de 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 el 1 de Feb. de 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 comentarios
Cris LaPierre
Cris LaPierre el 1 de Feb. de 2024
Note that the user account in the example also changes from 'XXX' to 'YYY"

Iniciar sesión para comentar.

Categorías

Más información sobre Search Path en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by