sprintf - Linux OK but Windows Warning: Escaped character '\.' is not valid. See 'doc sprintf' for supported special characters.
27 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Daniel Bridges
am 8 Feb. 2018
Beantwortet: M P
am 4 Mai 2020
I was using the following code inside a for loop to loop through multiple data folders and specify the folder as basically 'main\1', 'main\2', etc. on Ubuntu 16.04 Linux:
currentfolder = sprintf([DICOMdatafolder,'\%d'],loopPt);
It worked fine. In Windows 10, however:
Warning: Escaped character '\.' is not valid. See 'doc sprintf' for supported special characters.
Removing the \ causes the same warning, and so does the following:
currentfolder = sprintf(fullfile(DICOMdatafolder,'%d'),loopPt);
so I don't know what's wrong. Looking at the documentation, %d appears correct, and I don't understand what it is reading as an escape character. (I'm not sure what an escape character is, either: I thought it was used for syntax in a regular expression, but I didn't that was the case here.)
What is causing this warning and how do I resolve it to properly specify the folder as main/1, main/2, etc while looping through folders?
Below is a larger excerpt of the code:
DICOMdatafolder = fullfile('..','..','..','data','DICOMfiles');
NoPtfolders = CountNumberofFolders(DICOMdatafolder);
for loopPt = 1:NoPtfolders
currentfolder = sprintf([DICOMdatafolder,'%d'],loopPt);
Akzeptierte Antwort
Walter Roberson
am 8 Feb. 2018
Use sprintf('%s%d',DICOMdatafolder,loopPt)
Remember that the path separator for windows is \ so your fullfile call is going to contain ..\..\ in the output but on Linux would instead have ../..
When you use the result in the first parameter of sprintf you are asking it to use those \ characters in the format specification.
4 Kommentare
Walter Roberson
am 20 Feb. 2018
%d will never include the leading + unless you use %+d
The difference between %d and %u does not become important unless you are displaying uint64 that is larger than 2^54, and if you are then you had better be careful to explicitly make your for loop bounds uint64 as they default to double.
Weitere Antworten (2)
Julian Hapke
am 8 Feb. 2018
Bearbeitet: Julian Hapke
am 8 Feb. 2018
The warning you get should be:
Escaped character '\%' is not valid.
What you want is a platform dependent
filesep
between path and the number, so in your case (Windows)
sprintf([DICOMdatafolder,'\\%d'],loopPt);
or (Linux)
sprintf([DICOMdatafolder,'\/%d'],loopPt);
or better, to be platform independent
sprintf([DICOMdatafolder, filesep, '%d'],loopPt);
which is essentially what fullfile does (go and do
edit fullfile
to see what i mean.
The escape character is the "\", which in the case of sprintf is for example used with \n or \t for newline and tab. The "%" itself is a special character for sprintf, but if you want a litersal "%", the way to go is %% and not \% as one would do in RegExp (which is stated in the help of sprintf)
2 Kommentare
Stephen23
am 20 Feb. 2018
Bearbeitet: Stephen23
am 20 Feb. 2018
sprintf([DICOMdatafolder, filesep, '%d'],loopPt);
results in the same error because \ is a special character called an escape character (just like %), and is not interpreted literally. What Julian Hapke showed you (but did not explain explicitly), is that to get an literal version of an escape character you need to double it (as explained in the sprintf help, and shown in Julian Hapke's examples). So you would need to do this:
sprintf([DICOMdatafolder, filesep, filesep, '%d'],loopPt);
Or avoid the whole problem by using a properly defined format string:
sprintf('%s%s%d',DICOMdatafolder,filesep,loopPt);
But I would definitely NOT recommend these as solutions! Use fullfile instead:
fullfile(DICOMdatafolder, sprintf('%d', loopPt))
M P
am 4 Mai 2020
Well guys,
I am reading all the answers and comments and I am thinking about my problem as described in this question. What would you do if you did not write ANY "escape" character (eg. \U), but your script did not want to run because of the error message as mentioned in my question? Any wise ideas?
Regards
MP
0 Kommentare
Siehe auch
Kategorien
Mehr zu Filename Construction finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!