English 中文(简体)
无法读到肉类中的固定湿度(6E12)。
原标题:Unable to read fixed width (6E12) numbers in matlab.

I m having trouble reading some fixed width (6E12) data from a text file. Notice that there are leading spaces in the file.

The first line isnt that important. However, starting on the 2nd line I want to read 59 values.

file.txt

 PleaseAnswer    3 ,   ThisQuestion    40
 0.00000E+00 4.78181E+01-4.76356E+01 3.76280E-01 0.00000E+00 1.59238E+00
 1.88171E+00 1.73928E-06 0.00000E+00 3.57826E+01 0.00000E+00 0.00000E+00
 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
 0.00000E+00 3.75261E-07 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
 0.00000E+00 0.00000E+00 0.00000E+00 3.28103E+01 0.00000E+00 0.00000E+00
 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
 ...

end file.txt

如下文所示,由于负面迹象,我遇到第2和第3数值的问题。 我完全理解为什么要有一个主要空间。 我的几乎所有数字都应有一个主要空间,但消极的空间除外。

me trying to read this file

fid = fopen(filename);
dontcare = textscan(fid,  %s %d %s %s %n , 1);
fixme = textscan(fid,  %12s , 59,  Delimiter ,   ,  Whitespace ,   );
fixme{1}

ans = 

      0.00000E+00 
     4.78181E+01- 
     4.76356E+01  
     3.76280E-01  
     0.00000E+00  
     1.59238E+00 
      1.88171E+00 
     1.73928E-06  
     0.00000E+00  
     3.57826E+01  
     0.00000E+00  
     0.00000E+00 
      0.00000E+00 
     0.00000E+00  
     0.00000E+00  
     0.00000E+00  
     0.00000E+00  
     0.00000E+00 
      0.00000E+00 
    ...

end me trying to read file

问题回答

You can tokenize using regular expressions. This solution uses a dyamic array, so if you read in a large file you ll want to get the line count a priori to pre allocated. Not the most elegant, but it works.

fid = fopen( file.txt , r );

line = fgetl(fid); % ignore first line
l=1;
done = false;
 while ~done
    line = fgetl(fid); 
    if line ~= -1 %empty line is returned
       [s,e] = regexp(line,  -?[0-9]+.[0-9]+E[+-][0-9]+ , start , end ); %not sure why tokens aren t working
       for i=1:numel(s)
          data(l,i) = str2double(line(s(i):e(i))); %grab the ith match between start and end
       end
        l=l+1;
    else
        done = true;
    end
 end

 data

<<>Outputs>

>> test

data =

         0   47.8181  -47.6356    0.3763         0    1.5924
    1.8817    0.0000         0   35.7826         0         0
         0         0         0         0         0         0
         0    0.0000         0         0         0         0
         0         0         0         0         0         0
         0         0         0         0         0         0
         0         0         0   32.8103         0         0
         0         0         0         0         0         0
         0         0         0         0         0         0
         0         0         0         0         0         0




相关问题
Which non-bold UTF-8 iPhone SDK font can I use here?

Problem: I create a UILabel with this font: label.font = [UIFont systemFontOfSize:15.0f]; Now this system font is bold, but I want to display pretty small text, and the text may also contain very ...

Limiting file upload type

Simple question. Is there a way to only allow txt files upon uploading? I ve looked around and all I find is text/php, which allows PHP. $uploaded_type=="text/php

Extract everything from PDF [closed]

Looking for solution to extract content from a PDF file (using console tool or a library). It will be used on server to produce on-line e-books from uploaded PDF files. Need to extract following ...

Flash & external form fields

Does anybody know if this is possible? I am trying to create a flash movie that will show / preview what I am typing into a field in a normal HTML form. The call to update the flash movie would most ...

Avoiding escape sequence processing in ActionScript?

I need to mimic C# functionality of the @ symbol when it precedes a string. @"C:AFilePath" for example What is the best way to do this? Also there are some sites that will escape larger ...

iPhone: Draw rotated text?

I want to draw some text in a view, rotated 90°. I m pretty new to iPhone development, and poking around the web reveals a number of different solutions. I ve tried a few and usually end up with my ...

Numbering in jQuery

How could I change the text below so that the text within it has a number appended to it. <div class="right">This is some text</div> <div class="right">This is some text</div>...

热门标签