sprintf - Linux OK but Windows Warning: Escaped character '\.' is not valid. See 'doc sprintf' for supported special characters.

99 views (last 30 days)
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);
  1 Comment
Daniel Bridges
Daniel Bridges on 8 Feb 2018
This warning is resolved if I change the code instead to the following:
currentfolder = fullfile(DICOMdatafolder,num2str(loopPt));
Perhaps trying sprintf was overkill in this case. However, I would appreciate an answer to learn more about what the warning means, what caused it, and how to resolve it.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 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 Comments
Walter Roberson
Walter Roberson on 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.

Sign in to comment.

More Answers (2)

Julian Hapke
Julian Hapke on 8 Feb 2018
Edited: Julian Hapke on 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 Comments
Stephen23
Stephen23 on 20 Feb 2018
Edited: Stephen23 on 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))

Sign in to comment.


M P
M P on 4 May 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

Community Treasure Hunt

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

Start Hunting!