matlab - inaccurate reading of data by sscanf -


In the numerical representation in a double array, the conversion of string representation of numbers in cell array strobe double Or combination of str2num and cellphone . Although using a combination of str2mat and sscanf to get a faster conversion, this approach is failing for some data sets, such as the following:

  x = {'98.78743 '; '99.339717 '; '99.997878 '; '100.40125'; '100.79166'; '101.10525'; '101.34,037'; '101.4 9, 553'; '101.56, 939'; '101.56,072'; '101.4685'; '101.2 9, 184'; '101.03,002'; '100.68,249'; '100.24,887'; '99 .72,897'; '99.2274'; '9 8.43,036'; '97 0.65215 '; '9 6.78,864'; '9 5.84,054'}; Y = sscanf (str2mat (x) ','% f '.); [Str2double (x) -y] ans = 98.7874 99.3 9 72 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.5607 0.5607 101.0000 101.4685 0.4685 101.0099 101.2918 101.2918 -0.0000 101.0300 0.0300 101.0000 100.6825 0.6825 100.0000 100.248 9 0.2489 100.0000 99.7290 0.7290 99.0000 99.1227 99.1227 98.4304 98.4304 97.6522 97.6522 96.7886 96.7886 95.8405 95.8405 0 The last line in the code above is the block Indicates the double presentation of the original cell array, the difference between  sscanf  - the change in appearance, and the two For a loyal conversion, all the entries in the third column should be OK. Most of the values ​​are converted with the inefficient elimination of the most important digits. What is the basis of this wrong behavior and can the statement be slightly modified for correct conversion? In general, the numbers I am trying to change are of different length (both pre- and post-decimal point), so any approach depends on all the numbers in the cell array, which are of equal length Fail. Finally, my version of MATLAB is pre-2013, so I can not trust the  strjoin  approach which was suggested in the post above too. 

  y = sscanf (str2mat (x). ','% 9f ')    9  actually corresponds to the number of maximum lengths in the  str2mat (x). ' 

  & gt; & Gt; Str2mat (x). 'Ans = 999111111111111999999 8990000000000000000998765 ... 001111111100 ...... 739 ............ 714678 893471345542062223584 775090496669384820280 417115059081028973165 378262353758048746544 5657392 4297  

Therefore, Sscanf 9 will search for floats and will prevent it from receiving a position. You can define 9 values ​​depending on your data.

Example:

  x = {'98.7 '; '99.3397 '; '99.997878 '; '100.4'; '100.7 9, 166'; '101.10'}; & Gt; & Gt; Str2mat (x). 'Ans = 999111 89 9 000 ... 001 739 ... 93471 75 90 7 1 6 6 6  

Here the maximum length is 9. And the answer is still right.

  y = sscanf (str2mat (x). ','% 9f ') & gt; & Gt; Y = 98.7000 99.3 9 70 99.9358 100.4000 100.7917 101.1000  

To set the random length, use it:

  size (str2mat (x).), 1)  

and another move:

  xMaxLength = num2str (size (str2mat (X). ', 1)); Y = sscanf (str2mat (x). ', ['% 'XMaxLength' f ']);  

To compile only once str2mat :

  xx = str2mat (x). '; XxMaxLength = num2str (size (xx, 1)); Y = sscanf (xx, ['%' xxMaxLength 'F']);  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -