making multiple copies of a file
15 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
Walter Roberson
2011-9-1
Looks like I left off a close bracket:
destnames = cellstr(num2str((1:numcopies).', filepattern));
更多回答(3 个)
Amith
2012-12-26
will this work
copyfile('output2.txt',destnames(i));
1 个评论
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
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?
0 个评论
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!