Help reading and cleaning data from text file
    조회 수: 9 (최근 30일)
  
       이전 댓글 표시
    
Hello everyone,
I am currently having difficulties trying to read a text file in Matlab. A small example is shown below.
My aim is to;
-Open the file
-Ignore the header (first n lines)
-Replace the Bad entries with NaN
-Replace any corrupted entries with NaN (as entry (3,1))
----
I tried the following code with which I had some success if I removed the header first but I am struggling to get it all to work together.
buffer=fileread('C:\Users\Jack\Desktop\ContourGT\13.07.2013_header_removed.txt);
buffer = strrep(buffer, 'Bad', 'NaN') ;
surface=sscanf(buffer, '%f',[width length]);
---
Wyko ASCII Data File Format 0 0 1
X Size 640
Y Size 3015
Block Name Type Length Value
Wavelength 7 4 200.746124
Bad Bad Bad Bad Bad Bad
Bad -15216.897461 Bad Bad Bad Bad
-15s83.3t93555 Bad Bad Bad Bad Bad
Bad Bad -15169.881836 Bad Bad Bad
Bad Bad Bad Bad Bad Bad
Bad Bad Bad Bad Bad Bad
---
Any ideas? Any thoughts would be gratefully appreciated.
Thanks in advance
Jack
댓글 수: 0
채택된 답변
  dpb
      
      
 2013년 8월 30일
        >> fmt=repmat('%s',1,6);
>> c=textscan(fid,fmt,'delimiter',' ','headerlines',5,'collectoutput',true);
>> fid=fclose(fid);
>> strrep(c{:},'Bad','NaN')
ans = 
'NaN'            'NaN'           'NaN'           'NaN'    'NaN'    'NaN'
'NaN'            '-15216.897461' 'NaN'           'NaN'    'NaN'    'NaN'
'-15s83.3t93555' 'NaN'           'NaN'           'NaN'    'NaN'    'NaN'
'NaN'            'NaN'           '-15169.881836' 'NaN'    'NaN'    'NaN'
'NaN'            'NaN'           'NaN'           'NaN'    'NaN'    'NaN'
'NaN'            'NaN'           'NaN'           'NaN'    'NaN'    'NaN'
>>
Now either use regexp or cellfun w/ a pattern on the cell content to replace any string that isn't all numeric w/ 'NaN' as well. Then you can use str2num()
The only assumption is that the corruption is limited as your sample so that there are a consistent number of fields per record.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!