fprintf with warning of a lone trailing backslash

조회 수: 26 (최근 30일)
Andreas
Andreas 2022년 9월 28일
댓글: Walter Roberson 2022년 10월 12일
Hi,
I have used matlab earlier but forgotten most of it. I have a bunch of scripts from earlier co-workers and there is some warnings. Probably because the code is from 2015-2017.
This code produces a warning 'A lone trailing backslash, ' \ ', is not a valid control character.
for m = 1:M
folderName = strcat(rootFolder,timeSteps(m+2).name,filesep);
fprintf(['\nProcessing folder', folderName]);
As I am 'new' to this it is hard for me to understand how I should fix it. I assumed the problem is '\n..', so I tried to remove that but I still get the warning and I am not sure what removing the \n does in this case. The script seems to work anyway, but I would like to know what is wrong.
Hope someone can help me

채택된 답변

Image Analyst
Image Analyst 2022년 9월 29일
Like @Walter Roberson said, use fullfile, not strcat.
folderName = fullfile(rootFolder, timeSteps(m+2).name);
fprintf('Processing folder #%d of %d : "%s".\n', m, M, folderName);
Also see attached demo if you want to scan subfolders and do something to files in each subfolder.
  댓글 수: 1
Andreas
Andreas 2022년 10월 5일
Yes! This solution is best. Thanks a lot for suggesting fullfile and not strcat. Thank you all for being helpfull!

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

추가 답변 (3개)

VBBV
VBBV 2022년 9월 28일
folderName = strcat(rootFolder,filesep,timeSteps(m+2).name);
fprintf(['\nProcessing folder %s', folderName]);
You need to interchange the filesep position to middle or center to distinguish names. Plus use the format specifier as well in fprintf
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2022년 9월 28일
+1! filesep at the end of the string is the root cause of the warning!
Andreas
Andreas 2022년 9월 29일
I tried changing filesep. That gives me an error:
'Processing folder ../output/\002Error using imread>get_full_filename (line 570)
File "../output/\002DEM.tif" does not exist.'
So this solution does not work.
The best solution so far is
folderName = strcat(rootFolder,timeSteps(m+2).name, filesep);
fprintf('\nProcessing folder %s', folderName);

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


Fangjun Jiang
Fangjun Jiang 2022년 9월 28일
Change to
fprintf(['\nProcessing folder %s', folderName]);
'\n' is fine, it produces a carriage return
Just need the '%s' to print the value of folderName.
  댓글 수: 6
Fangjun Jiang
Fangjun Jiang 2022년 9월 28일
@VBBV 's answer points to the root cause of the warning. It is the filesep ('\' in Windows) at the end of folderName.
So, for @Andreas, the true fix (with minimum change to the original code) should be this
folderName = strcat(rootFolder,filesep,timeSteps(m+2).name);
fprintf(['\nProcessing folder', folderName]);
Walter Roberson
Walter Roberson 2022년 9월 29일
We recommend that you use fullfile()

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


Jasmine Dhaliwal
Jasmine Dhaliwal 2022년 10월 12일
(I know your question is already answered, but since they mentioned that there's %d and %s, I wanted to make sure you also knew that there is %f for float numbers instead of doubles (%d)! How it works: You can specify how many digits and how many decimals you want with something like %4.2f, where 4 indicates a field width of 4 digits and .2 indicates how many decimals you want! In my class we really mainly used %0.2f iirc, so I certainly looked up the literature on what the first digit (4, in this case) meant, because I couldn't quite remember what it meant. As it is, I really couldn't tell you what "field depth" is, but hopefully this is helpful to you anyhow!
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 10월 12일
the number before the % is the minimum field width. It is the target width for a column of numbers. The default is right jurisdiction within the column but the "-" flag requests left justification and the "0" flag requests zero padding on the left instead of space padding. The field will use more characters if necessary: it is common, for example, for people to forget to take into account the space for a negative sign.

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

카테고리

Help CenterFile Exchange에서 File Name Construction에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by