Build a vector of file names with formatting and leading zeros

6 views (last 30 days)
How can I build a vector of file names with format "sprintf('myfile_%02d_%03d.txt', firstInteger, secondInteger)" where "firstInteger" is a 1-2 digit and "secondInteger" is a 2-3 digit are positive integer such that the former has a eading 0 if it were 1 digit and the latter has a leading 0 if it were 2 digits. If I have all "N" integers pairs "firstInteger" and "secondInteger" in the rows of an Nx2 matrix FileNameIntegersMatrix = [firstInteger1 secondInteger1; firstInteger2 secondInteger2, ..., firstIntegerN secondIntegerN], is there a fast and efficient way to build all the files names in a vector with format such as below where each row is a string of the file name with the format shown above.
FileNames = [sprintf('myfile_%02d_%03d.txt', firstInteger1, secondInteger1) ; sprintf('myfile_%02d_%03d.txt', firstInteger2, secondInteger2) , ..., sprintf('myfile_%02d_%03d.txt', firstIntegerN, secondIntegerN)]

Accepted Answer

Matt J
Matt J on 4 Aug 2021
Edited: Matt J on 4 Aug 2021
FileNames = compose('myfile_%02d_%03d.txt', ...
FileNameIntegersMatrix(:,1), FileNameIntegersMatrix(:,2) )

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 4 Aug 2021
You can try using strcat() that works quite efficiently, e.g.:
A = 'My_file_'; B='_';
firstInteger1=10; secondInteger1=20;
Filenames = strcat(A, num2str(firstInteger1), B, num2str(secondInteger1))
Filenames = 'My_file_10_20'

Community Treasure Hunt

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

Start Hunting!