how to read a data file with textscan when the the data type is inconsistant
2 views (last 30 days)
Show older comments
Hi all,
I am trying to read a data file containg 100000 lines look as follow:
2.40412 0.45270 3.22916 0.00000 0.00000 2276.6 -1.000 0.54 0 0 Ni
1.92015 2.45198 5.47533 0.02500 0.00000 2847.2 0.543 1.25 1 1 Ni
so my script is: MTRX=textscan(fid, '%f %f %f %f %f %f %f %f %f %f %s','delimiter','\n')
it is working fine but the data file sometimes contains line like:
1.#QNAN 1.57080 1.57080 0.05000 0.00000 0.0 -1.000 180.00 0 0 Ni
So textscan stops reading #QNAN and afterwards. I want to read 1.#QNAN as 1.
How I can get rid of #QNAN using textscan.
Cheers
Hossein
0 Comments
Answers (3)
Matt Tearle
on 22 Mar 2011
A simple hack workaround would be to read the first column as a string, then remove anything non-numeric from that string and convert to numeric:
MTRX=textscan(fid, '%s %f %f %f %f %f %f %f %f %f %s')
MTRX{1} = str2double(regexprep(MTRX{1},'[^.\d]',''))
0 Comments
Oleg Komarov
on 22 Mar 2011
Couldn't find a way, then 2 solutions:
1) substitute in you file .#QNAN with empty '' and import afterwards, replaceinfile could be useful.
2) Import as sting and manipulate content of first column:
fid = fopen('C:\Users\Oleg\Desktop\data.txt');
MTRX = textscan(fid, '%s %f %f %f %f %f %f %f %f %f %s');
fid = fclose(fid);
temp = regexprep(MTRX{1},'.#QNAN','');
[str2double(temp) MTRX{2:end-1}]
2 Comments
Matt Tearle
on 22 Mar 2011
As an alternative version of (1) (without needing replaceinfile):
txt = fileread('filename.txt');
txt = regexprep(txt,'#QNAN','00000');
MTRX = textscan(txt, '%f %f %f %f %f %f %f %f %f %f %s');
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!