Using a specific number of digits
Afficher commentaires plus anciens
I am looking for a way to fix the number of digital in an array. I am attempting to run a for loop in the form "for k = 1:N where n will be an interger typically between 10 and 200. However as it counts through the loop it will report k as 1, 2, 3 etc. I would like to force k to be three digits such as k = [001 002 003.....010 011 012......100 101 102]
Is there a simple way to do this?
Thanks you
Réponse acceptée
Plus de réponses (1)
Jeff Miller
le 28 Oct 2022
Maybe by "report k" you are referring to printing it, in which case this should work
for k=1:100
fprintf('%03d\n',k)
end
4 commentaires
John Carroll
le 29 Oct 2022
Jeff Miller
le 29 Oct 2022
for a file name use sprintf, something like this
fname = ['MyFile' sprintf('%03d\n',k) '.mat'];
Stephen23
le 29 Oct 2022
Much better without the text concatenation and newline character:
fname = sprintf('MyFile%03d.mat',k);
John Carroll
le 31 Oct 2022
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!