Textscan issues
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Titus Edelhofer
le 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
Plus de réponses (1)
Paul
le 4 Jan 2012
2 commentaires
Matt Tearle
le 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.
Voir également
Catégories
En savoir plus sur Data Type Identification dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!