[英]Leading zeros in Matlab char array
I have an integer array that has zeros in it, but when I convert it into a char array, the leading zeros are gone. 我有一个整数数组,其中有零,但是当我将其转换为char数组时,前导零消失了。 How can I have leading zeros in a char array? 如何在char数组中使用前导零?
For example: it prints ' 4D9'
instead of '004D9'
. 例如:它打印' 4D9'
而不是'004D9'
。 How do I print the leading zeros? 如何打印前导零?
Assuming you mean to print the integers in hexadecimal with leading zeros, you just need the leading 0
flag in the formatspec
: 假设您的意思是将十六进制整数以前导零打印, formatspec
需要formatspec
的前导0
标志:
>> n = 1241;
>> s = num2str(n,'%05X')
s =
004D9
I go through each element in the number array and convert it to a string. 我遍历了数字数组中的每个元素,并将其转换为字符串。 I then concatenate these together 然后将它们连接在一起
a = [0,0,2,3,4,5,6];
my_str = '';
for ii=1:numel(a)
my_str(ii) = num2str(a(ii));
end
disp(my_str)
this gives the output '0023456' 这给出了输出'0023456'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.