필터 지우기
필터 지우기

how to create multiple blank txt files in a folder

조회 수: 8 (최근 30일)
Jin
Jin 2012년 4월 25일
댓글: Walter Roberson 2020년 2월 4일
suppose I want to create 100 blank txt files and name them as file1.txt, file2.txt .... , how to write the command
I tried to write a loop and use fopen with option w+, does not work
  댓글 수: 1
Jan
Jan 2012년 4월 25일
"Does not work" is not useful to describe a problem in a forum. Please add any details: Does your program fail? With an error message? Which line? Post the relevant part of the code. Or does the result differ from your expectations? Perhaps you do not have write access for this folder? Or the drive is full?

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

답변 (1개)

Matthew
Matthew 2012년 4월 25일
Hi Jin,
You could use something like
for ii = 1:100
file_name = ['file' sprintf('%1.0f',ii) '.txt'];
fopen(file_name,'w+');
end
  댓글 수: 3
Divy Gupta
Divy Gupta 2020년 2월 3일
I wanted to write after fopen using fprintf but in the files data was not available.I write something like
for ii = 1:100
file_name = ['file' sprintf('%1.0f',ii) '.txt'];
fopen(file_name,'w+');
fprintf(file_name, '%d', myarrray);
end
Walter Roberson
Walter Roberson 2020년 2월 4일
You did not remember to fclose and you do not use file identifiers properly. The contents of a file are not guaranteed to be completely written to disc until the fclose() has finished.
for ii = 1:100
file_name = sprintf('file%d.txt',ii);
fid = fopen(file_name,'w+');
fprintf(fid, '%d ', myarrray); %you need a delimiter or the numbers will run together
fprintf(fid, '\n');
fclose(fid)
end

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by