Build a vector of file names with formatting and leading zeros

6 次查看(过去 30 天)
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)]

采纳的回答

Matt J
Matt J 2021-8-4
编辑:Matt J 2021-8-4
FileNames = compose('myfile_%02d_%03d.txt', ...
FileNameIntegersMatrix(:,1), FileNameIntegersMatrix(:,2) )

更多回答(1 个)

Sulaymon Eshkabilov
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'

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by