フィルターのクリア

Write to text file - For loop with newline

2 ビュー (過去 30 日間)
RuiQi
RuiQi 2016 年 6 月 5 日
回答済み: Walter Roberson 2016 年 6 月 5 日
I am reading the names of all the images and then I want to write the name to a text file with a newline. Like
pic1.png
pic2.png
But my code below cannot new line. How to new line help thanks.
imagefiles = dir('*.jpg');
nfiles = length(imagefiles); % Number of files found
fileID = fopen('Label.txt','wt');
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
name = sprintf(currentfilename,'\r\n');
fprintf(fileID,name);
end
fclose(fileID);

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 6 月 5 日
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
fprintf(fileID, '%s\n', currentfilename);
end
Do not use \r\n however. You opened the file with 'wt' which is text mode, and if you are on an MS Windows system that automatically means that all \n should be transformed into \r\n when you write them. If you might be executing the code on non-MS Windows systems and you need to force \r\n then you should be opening in binary mode 'w' instead of 'wt' and in that case do use \r\n

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by