Using a specific number of digits
Show older comments
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
Accepted Answer
More Answers (1)
Jeff Miller
on 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 Comments
John Carroll
on 29 Oct 2022
Jeff Miller
on 29 Oct 2022
for a file name use sprintf, something like this
fname = ['MyFile' sprintf('%03d\n',k) '.mat'];
Stephen23
on 29 Oct 2022
Much better without the text concatenation and newline character:
fname = sprintf('MyFile%03d.mat',k);
John Carroll
on 31 Oct 2022
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!