Help reading and cleaning data from text file

4 views (last 30 days)
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

Accepted Answer

dpb
dpb on 30 Aug 2013
>> 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.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!