Help reading and cleaning data from text file
3 次查看(过去 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 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!