필터 지우기
필터 지우기

making multiple copies of a file

조회 수: 22 (최근 30일)
Ram
Ram 2011년 9월 1일
답변: Sam Apoola 2019년 2월 14일
I would like to make multiple copies of a file each of which will have a different name. example: source file: A.txt copies A1.txt, A2.txt... How can I use copyfile and be able to do this? Thanks for inputs

채택된 답변

Walter Roberson
Walter Roberson 2011년 9월 1일
copyfile() can only create one copy at a time, so you will need a loop (whether explicit or implicit)
One of numerous possible ways:
sourcefile = 'A.txt';
numcopies = 20;
[path, basename, ext] = fileparts(sourcefile);
filepattern = fullfile(path, [basename '%d.' ext]);
destnames = cellstr(num2str((1:numcopies).', filepattern);
cellfun(@(FID) copyfile(sourcefile, FID), destnames);
  댓글 수: 4
Ram
Ram 2011년 9월 1일
I have an error that they are too namy input arguments in the use of the function cellstr. Do you know what might be causing it.
thanks
Walter Roberson
Walter Roberson 2011년 9월 1일
Looks like I left off a close bracket:
destnames = cellstr(num2str((1:numcopies).', filepattern));

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

추가 답변 (3개)

Amith
Amith 2012년 12월 26일
will this work
copyfile('output2.txt',destnames(i));
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 12월 26일
No, destnames here is a cell array of strings, so destnames(i) would be a 1 x 1 cellarray, rather than a string. If you used destnames{i} then that would be a string.
You would need to loop "i" over all of the output string possibilities. The cellfun() that I show is responsible for that.

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


Diana Krupnik
Diana Krupnik 2019년 1월 28일
This solution names the files by numbering them, would it be possible to instead change the names based on a table that contains string or text?

Sam Apoola
Sam Apoola 2019년 2월 14일
This worked for me
% loop for creating 100 copies
n=100;
for i=1:n
jobname{i}= ['copy', num2str(i),'.txt'];
copyfile('original.txt',jobname{i});
end

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by