Why does textscan read only 95% of my data file?
Mostrar comentarios más antiguos
My dat file has 423,000+ ascii lines that look like:
2017-08-30 12:34:56 7.89
When I use textscan, each of the 7 cells in the returned variable only have 413315 values. If I do it line by line with fgetl, I get all 423,000+ values. Textscan takes a few seconds. fgetl takes several minutes.
DATAX=textscan(fid,'%d-%d-%d %d:%d:%d %f');
Next thing to try is to sed replace '-', ':', and ' ' with \t then try again. Unfortunately I have almost 400 files like this. An opportunity to improve my shell scripting...
Any help is greatly appreciated.
2 comentarios
Janice Nelson
el 12 de Sept. de 2017
Editada: Janice Nelson
el 12 de Sept. de 2017
Janice Nelson
el 12 de Sept. de 2017
Respuesta aceptada
Más respuestas (3)
Walter Roberson
el 12 de Sept. de 2017
filecontent = fileread(TheFileName);
tokens = regexp(filecontent, '^(?<date>[-0-9]+\s+[0-9:.]+)\s+(?<value>-?[0-9.]+)', 'names');
dates = datetime({tokens.date});
values = str2double({tokens.value});
This code has been designed to permit negative numeric values, but it does assume that if there is a negative sign then the numeric values are immediately afterwards with no space. Also, this code is not designed to recognize exponential format.
5 comentarios
Janice Nelson
el 12 de Sept. de 2017
Editada: Janice Nelson
el 12 de Sept. de 2017
Janice Nelson
el 12 de Sept. de 2017
Does not
fmt ='%{yyyy-MM-dd}D %{HH:m:ss}D %f');
data=textscan(fid,fmt %f');
work? (If the dates are poorly formatted as 13: 4:29 it will fail as Walter notes but one would hope that isn't so...)
If are, use
fmt='%q %q %f');
data=textscan(fid,fmt,'collectoutput',1);
dates=datetime(strjoin(data{:,1});
Walter Roberson
el 12 de Sept. de 2017
When you use a %D for times within a day but without the date, then the result is taken relative to the date on which it was scanned. You cannot then just add that to the date portion. You have to do things like:
DatePortion + (TimePortion - shiftdate(TimePortion, 'start', 'day'))
That's what strjoin is for--concatenate the date/time strings into one for datetime to parse as a whole.
I think it's a major wart in implementation of '%D' that it doesn't handle the cases directly; I'm hoping that's because it's still the new kid on the block and just isn't yet ripe but like wine will improve with aging...
Excepting owing to the cell structure it won't work as written as that will concatenate all elements in order of all dates followed by all times in one long string...it's a pain to deal with no matter what you do.
dpb
el 12 de Sept. de 2017
0 votos
There'll be a formatting discrepancy in the file at the offending line that causes textscan to fail. fgetl otoh reads the line as character string without formatting it so content is totally immaterial.
Since what you have is a date/time field, I'd suggest using the '%D' format string and return the data as datetime class instead of a string of variables. See the format field description for details on the format.
You might attach the last portion of the file with the offending line so folks can see what might be the actual issue -- only the section in the neighborhood of the place where the code fails is pertinent.
1 comentario
Walter Roberson
el 12 de Sept. de 2017
The %D format specifier is a bit tricky because spaces cannot be present in the date, unless you have set 'whitespace' to exclude space. However if you set 'whitespace' to exclude space then it is not going to be able to recognize the spacing between the time and the value.
Jeremy Hughes
el 12 de Sept. de 2017
Editada: Jeremy Hughes
el 13 de Sept. de 2017
textscan can read time-of-day as datetime,
having the format
d = textscan(fid,'%D%D%f','Delimiter',' ','ReturnOnError',false);
might work. The you'd have to do something like this to post-process:
[date,time,n] = d{:};
date = date + timeofday(time);
textscan will simply stop reading when it encounters an error. To see what data is messing up the read, setting ReturnOnError=false will issue an error instead of just stopping. This should give an indication of the problem.
1 comentario
dpb
el 13 de Sept. de 2017
It would be most interesting if OP would return and show us the offending record in the file to see just what broke what...
Are there plans to fix some of the observed warts with '%D' in the future with the embedded blank issue, etc., ... ?? It's a strong step forward but there are still "issues" that it doesn't handle well as well as the rest of the datetime class.
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!