Using a specific number of digits
11 ビュー (過去 30 日間)
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?
John D'Errico 2022 年 10 月 28 日
編集済み: John D'Errico 2022 年 10 月 28 日
Are numbers typically written with leading zero digits? (NO. At least not in anything I've ever done.) The number 001 is no different from the number 1. As such MATLAB returns it as that. No leading digits, at least not if you want it in a numeric form.
Can you force MATLAB to convert an integer so it always return leading zero digits if they would be there? Not as a number. Sorry. But if you want the result to be in a character form, you could do this:
x = 12;
That will force it to always prepend leading zero digits, so there are always 3 digits shown. HOWEVER, the result is NOT a number. It is just a string of characters at this point..