Help reading and cleaning data from text file
1 回表示 (過去 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 で String Parsing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!