Code won't run on Mac due to backslashes

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 commentaires

Stephen23
Stephen23 le 15 Jan 2025
Modifié(e) : Stephen23 le 15 Jan 2025
"There are dynamic paths and absolute paths in the code which are written for Windows (with backslash)."
Why? Send the code back to the author and tell them to fix their badly written code.
"I wanted to run a script which will replace all backslashes with forwardslashes in all the .m and .mat files"
So rather than fixing the actual problem you want to instead write some kind of dodgy script that might or might not work correctly and would require a non-trivial amout of time to develop and test...
"I feel it is a simple issue with a complex solution and it should not exist at all."
You are right, this situation should not exist at all! Which is exactly why FULLFILE and FILESEP exist: so that users can easily write code that generalises (as much as possible) to work on all OSs. But MATLAB cannot force users to use those functions, so if a user really really really wants to write bad code with hard-coded back slashes... well, that is their choice.
The best solution is to fix the code, not to write a magic meta-programming script.
Or use forward slashes, which work on all OSs that MATLAB currently runs on.
Multiple OSs exist. This is why programmers invented and use functions like FULLFILE, exactly so that they do not have to worry about such things. They certainly do not go about writing "scripts" to mangle their code every time they need to run it on another OS.
Use FULLFILE.
Nikola
Nikola le 16 Jan 2025
I was given a code by a professor to use for writing a thesis. And the code is not written by him. So there is no chance of having it rectified by someone else. If I have time I may go and re-write everything with "fullfile".
I would say that the first thing to do is to convert \ to / in most places. / is accepted by Windows as well.
Nikola
Nikola le 16 Jan 2025
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.

Connectez-vous pour commenter.

Réponses (1)

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 commentaire

Nikola
Nikola le 16 Jan 2025
Thank you for answering. I converted them manually in the end.

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Tags

Question posée :

le 15 Jan 2025

Commenté :

le 16 Jan 2025

Community Treasure Hunt

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

Start Hunting!

Translated by