Code won't run on Mac due to backslashes
Afficher commentaires plus anciens
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
"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
le 16 Jan 2025
Walter Roberson
le 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.
Nikola
le 16 Jan 2025
Réponses (1)
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.
1 commentaire
Nikola
le 16 Jan 2025
Catégories
En savoir plus sur File Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!