sscanf读取的数据不正确 [英] inaccurate reading of data by sscanf
问题描述
在Matlab中,可以使用str2double
或str2num
和cellfun
的组合将单元格数组中的数字的字符串表示形式转换为双精度数组中的数字表示形式. 最近的帖子建议结合使用str2mat
和sscanf
来加快转换速度.但是,这种方法对于某些数据集似乎无效,例如:
In Matlab, conversion of string representations of numbers in a cell array into numeric representations in a double array can be performed with str2double
or a combination of str2num
and cellfun
. A recent post suggested using a combination of str2mat
and sscanf
to achieve faster conversion. However, this approach seems to fail for certain data sets, such as the following:
x={'98.78743';'99.39717';'99.93578';'100.40125';'100.79166';'101.10525';'101.34037';'101.49553';'101.56939';'101.56072';'101.4685';'101.29184';'101.03002';'100.68249';'100.24887';'99.72897';'99.12274';'98.43036';'97.65215';'96.78864';'95.84054'};
y=sscanf(str2mat(x).','%f');
[str2double(x),y,str2double(x)-y]
ans =
98.7874 98.7874 0
99.3972 99.3972 0
99.9358 99.9358 0
100.4013 100.4013 -0.0000
100.7917 0.7917 100.0000
101.1052 0.1053 101.0000
101.3404 0.3404 101.0000
101.4955 0.4955 101.0000
101.5694 0.5694 101.0000
101.5607 0.5607 101.0000
101.4685 0.4685 101.0000
101.2918 101.2918 -0.0000
101.0300 0.0300 101.0000
100.6825 0.6825 100.0000
100.2489 0.2489 100.0000
99.7290 0.7290 99.0000
99.1227 99.1227 0
98.4304 98.4304 0
97.6522 97.6522 0
96.7886 96.7886 0
95.8405 95.8405 0
以上代码块的最后一行显示了原始单元格数组的双重表示形式,sscanf
转换后的形式以及两者之间的区别.为了忠实地进行转换,第三列中的所有条目都将恰好为0.许多值在转换时不正确地消除了最高有效位.这种错误行为的基础是什么,可以对语句做些微修改以产生准确的转换吗?通常,我尝试转换的数字具有不同的长度(小数点前和小数点后两者),因此,任何依赖单元格数组中具有相同长度的所有数字的方法都将失败.最后,我的MATLAB版本是2013年前的版本,因此我不能依赖上述帖子中建议的strjoin
方法.
The final line in the above code block shows the double representation of the original cell array, the sscanf
-converted form, and the difference between the two. For a faithful conversion, all entries in the third column would be precisely 0. Many of the values are converted with improper elimination of the most significant digits. What is the basis for this erroneous behavior and can the statement be modified slightly to produce an accurate conversion? In general, the numbers which I am trying to convert are of different lengths (both pre- and post-decimal point), so any approach that relies on all numbers in the cell array having the same length are bound to fail. Finally, my version of MATLAB is pre-2013, so I cannot rely on the strjoin
approach that was also suggested in the aforementioned post.
推荐答案
尝试以下方法:
y=sscanf(str2mat(x).','%9f')
9
实际上对应于str2mat(x).'
>> str2mat(x).'
ans =
999111111111111999999
899000000000000998765
...001111111100......
739............714678
893471345542062223584
775090496669384820280
417115059081028973165
378262353758048746544
5657392 4297
因此,sscanf
将搜索9个浮点,并在找到空格时停止.您可以根据数据定义9个值.
Hence, sscanf
will search for 9 floats and stop when it finds a space. You could define the 9 values depending on your data.
一个例子:
x={'98.7';'99.397';'99.93578';'100.4';'100.79166';'101.10'};
>> str2mat(x).'
ans =
999111
899000
...001
739...
93471
75 90
7 1
8 6
6
最大长度仍然是9.答案仍然正确.
Here the maximum length is still 9. And the answer is still correct.
y=sscanf(str2mat(x).','%9f')
>> y =
98.7000
99.3970
99.9358
100.4000
100.7917
101.1000
要确定%'number'f
的随机长度,请使用:
For determining the random length for the %'number'f
, use:
size(str2mat(x).',1)
另一个技巧:
xMaxLength = num2str(size(str2mat(x).',1));
y = sscanf(str2mat(x).',['%' xMaxLength 'f']);
为了只计算一次str2mat
:
xx = str2mat(x).';
xxMaxLength = num2str(size(xx,1));
y = sscanf(xx,['%' xxMaxLength 'f']);
这篇关于sscanf读取的数据不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!