Code won't run on Mac due to backslashes

조회 수: 19 (최근 30일)
Nikola
Nikola 2025년 1월 15일
댓글: Nikola 2025년 1월 16일
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
Walter Roberson
Walter Roberson 2025년 1월 16일
I would say that the first thing to do is to convert \ to / in most places. / is accepted by Windows as well.
Nikola
Nikola 2025년 1월 16일
Yes, you are right. I used a script which crawled recursively through all the files and listed those .m files which have backslash. So I went through each manually and changed it. In the end it was not that much, took me maybe half hour.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2025년 1월 15일
thing = 'filename\tthis\that';
fprintf(thing)
filename this hat
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)
filename this/that
instead of to
thing = 'filename/tthis/that';
fprintf(thing)
filename/tthis/that
??
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.
  댓글 수: 1
Nikola
Nikola 2025년 1월 16일
Thank you for answering. I converted them manually in the end.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by