Textscan issues

조회 수: 3 (최근 30일)
Paul
Paul 2012년 1월 4일
Once again, I ask your help to sort this out.
I'm trying to use texscan to open a .dat file. I want to ignore the 2 headerlines and I'm only interested in the first 5 columns, and the first two I define has beeing strings.
"location"
"DATA" "HORA" "VMED" "VMAX" "DIR" "TEMP" "PRESS"
14-08-1998 15:30 0.00* 0.00* 270.79* 18.71 1008.57
14-08-1998 16:00 1.49 2.69 272.55 18.43 1008.57
14-08-1998 16:30 1.34 2.09 262.00 18.80 1008.57
14-08-1998 17:00 1.72! 2.39 258.13 18.15 1008.74
14-08-1998 17:30 2.24 2.98 253.91 18.43 1008.74
14-08-1998 18:00 2.91 3.58 261.65 18.52 1008.57
Has you can see, some parameters are followed by a symbol ("*" and "!"), I want Matlab to write them has NaN.
I used the following code:
fid=fopen('/Users/...
v = textscan(fid,'%s %s %f %f %f %*s %*s, 'headerLines',2);
v3=v{3};
v4=v{4};
v5=v{5};
Unfortunately, Something is occuring when I try to create v4 has a double, since matlab only creates 3 empty matices. The only way to create the three matrices is to write:
v = textscan(fid,'%s %s %f %s %f %*s %*s, 'headerLines',2);
But that will create a matrix that I can not use.
On top of all that, a new row is being created at the beggining of the matrices, that does not correspond to the first row from the .dat file. Something like:
0
NaN
1,49
1,34
1,72
2,24
2,91
I really can't understand what is wrong! Please help

채택된 답변

Titus Edelhofer
Titus Edelhofer 2012년 1월 4일
Hi,
you are on the right way: read everything as strings:
v = textscan(fid, '%s %s %s %s %s %s %s', 'headerlines', 2);
and then do some manipulation to get the data you are looking for:
v3 = str2double(strrep(strrep(v{3}, '*', ''), '!', ''))
Titus
  댓글 수: 1
Paul
Paul 2012년 1월 4일
Thank you Titus, your solution worked beautifully!
Still can't figure out what happened. I believe my code should work!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Paul
Paul 2012년 1월 4일
Well, there's always one more problem!
How can I merge the two first columns into one? say I want to use textscan and tell him to ignore the delimiter between those columns so that I can later use Datenum.
Is it possible?
Thanks
Paul
  댓글 수: 2
Matt Tearle
Matt Tearle 2012년 1월 4일
If the date and time format is fixed, you can use a fixed-width field specifier: '%16c %s %s %s %s %s'. Then v{1} will be an array of date/time strings.
Titus Edelhofer
Titus Edelhofer 2012년 1월 4일
Hi Paul,
what about strcat(v{1}, v{2})?
Titus

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by