matlab中的如何把char转换为double
str2double给出的是45 33 4 3 2 1的ascii码表示。如何不要ascii 展开
2014-03-01
str2num可以 那为什么str2double不行呢
这个函数就是专门设计用于标量转换的,速度比str2num快,但只适用于标量。
请看函数文档里的说明:
X = str2double('str') converts the string str, which should be an ASCII character representation of a real or complex scalar value, to the MATLAB double-precision representation. The string can contain digits, a comma (thousands separator), a decimal point, a leading + or - sign, an e preceding a power of 10 scale factor, and an i for a complex unit.
If str does not represent a valid scalar value, str2double returns NaN.
1234>> s='45 33 4 3 2 1';>> <a href="https://www.baidu.com/s?
wd=str2num&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y4uhnYmyRkmW6YPyc4uW6d0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-
TLwGUv3EnHm1rj0LnjfvPjDsnWmLPW6zn0" target="_blank" class="baidu-highlight">str2num</a>(s)ans = 45 33 4 3 2 1
字符串型转换为数值型:
ss='010600001388849C';
ssDec = hex2dec(ss);
ssHex = dec2hex(ssDec);
format hex;
disp(ssHex);
a1 =
NaN
>> a2=str2num(a)
a2 =
45 33 4 3 2 1
>> class(a1)
ans =
double
>> class(a2)
ans =
double
以上说明,str2num就把char类转换为double类