MATLAB: Making multiple copies of a file

copyfile

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

Best Answer

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);