Code won't run on Mac due to backslashes
21 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a pretty extensive software consisting of multiple .m and .mat files. Code is supposed to have some processing done and results saved based on imported files. There are dynamic paths and absolute paths in the code which are written for Windows (with backslash). When I run the code on Mac, code crashes due to this. I wanted to run a script which will replace all backslashes with forwardslashes in all the .m and .mat files. Issue here is that there are also escape sequences (like newline and others) which must use backslash. So I can't run the script as it would mess up escape sequences. I fell back to running code on Windows machine. Is there really no chance of somehow setting execution on higher lever and ordering Matlab to always convert paths? I feel it is a simple issue with a complex solution and it should not exist at all.
4 Kommentare
Walter Roberson
am 16 Jan. 2025
I would say that the first thing to do is to convert \ to / in most places. / is accepted by Windows as well.
Antworten (1)
Walter Roberson
am 15 Jan. 2025
thing = 'filename\tthis\that';
fprintf(thing)
Suppose there was code that automatically detected filenames and converted them. How could that automatic process know to convert the above code to
thing = 'filename\tthis/that';
fprintf(thing)
instead of to
thing = 'filename/tthis/that';
fprintf(thing)
??
If there are any system() calls, how could automatic conversion calls know the difference between \ being directory separator and \ introducing command line switches? For that matter, command line switches in MacOS or Linux are typically introduced with either - or -- and how would the hypothetical conversion code know to convert them?
The parameters for a system() command or a dir() or an ls() can be built up in pieces. How can you expect code to be able to look ahead to see how strings are eventually going to be used to determine how to convert the code?
I recommend that you edit the code to parameterize filenames into variables, and to consistently use / as the seperator. Windows is happy with / as the seperator -- indeed, Windows internally uses / as the seperator and the use of \ is an overlay on top of that. And if necessary, code using
if ispc()
variablename = windows form
elseif ismac()
variablename = mac form
else
variablename = linux form
end
and code using fullfile() instead of strcat() or [] together filename pieces.
Siehe auch
Kategorien
Mehr zu File Operations 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!