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

조회 수: 97(최근 30일)
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
Daniel Bridges
Daniel Bridges 2018년 2월 8일
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.

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

채택된 답변

Walter Roberson
Walter Roberson 2018년 2월 8일
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
Walter Roberson
Walter Roberson 2018년 2월 20일
%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.

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

추가 답변(2개)

Julian Hapke
Julian Hapke 2018년 2월 8일
편집: Julian Hapke 2018년 2월 8일
The warning you get should be:
Escaped character '\%' is not valid.
What you want is a platform dependent
between path and the number, so in your case (Windows)
or (Linux)
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
Stephen23 2018년 2월 20일
편집: Stephen23 2018년 2월 20일
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:
But I would definitely NOT recommend these as solutions! Use fullfile instead:
fullfile(DICOMdatafolder, sprintf('%d', loopPt))

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

M P 2020년 5월 4일
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?

Community Treasure Hunt

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

Start Hunting!

Translated by