how to read a data file with textscan when the the data type is inconsistant
이전 댓글 표시
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
답변 (3개)
Walter Roberson
2011년 3월 22일
1 개 추천
textscan with CommentStyle {'#', 'AN'}
댓글 수: 1
Matt Tearle
2011년 3월 22일
Nice. I wondered about CommentStyle, but didn't think of that trick.
Matt Tearle
2011년 3월 22일
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]',''))
Oleg Komarov
2011년 3월 22일
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
Matt Tearle
2011년 3월 22일
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');
Hossein Alimadadi
2011년 3월 22일
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!