Textscan issues

3 views (last 30 days)
Paul
Paul on 4 Jan 2012
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

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 4 Jan 2012
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 Comment
Paul
Paul on 4 Jan 2012
Thank you Titus, your solution worked beautifully!
Still can't figure out what happened. I believe my code should work!

Sign in to comment.

More Answers (1)

Paul
Paul on 4 Jan 2012
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 Comments
Matt Tearle
Matt Tearle on 4 Jan 2012
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 on 4 Jan 2012
Hi Paul,
what about strcat(v{1}, v{2})?
Titus

Sign in to comment.

Categories

Find more on Data Type Identification 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!