Whitespace problem with textscan
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Stefán
el 8 de Oct. de 2014
Comentada: Stephen23
el 8 de Oct. de 2014
While using textscan, it doesn't seem to treat multiple whitespace as a single delimiter. The file has whitespace and tab delimeters,
2012-10-15 K01 5.83 5.05 5.73 6.41 4.28
2012-10-15 K01 5.25 5.80 6.41 4.28
2012-10-15 K01 4.28
Using data = textscan(fd, form,'HeaderLines',2); so data{:,3} becomes
5.83, 5.25 and 4.28
but should be
5.83, NaN and NaN.
I've tried using the whitespace function but cannot get it to work
Respuesta aceptada
Stephen23
el 8 de Oct. de 2014
Editada: Stephen23
el 8 de Oct. de 2014
It does if you use the 'MultipleDelimsAsOne' option:
This is explained in the section "Treat Repeated Delimiters as One".
Although, given your example output, you probably want to use the 'EmptyValue' option, which is explained in the section "Specify Delimiter and Empty Value Conversion".
8 comentarios
Stephen23
el 8 de Oct. de 2014
This works correctly:
>> S = sprintf('anna\t123.45\t \t67.890\nbob\t \t \t9.7');
>> textscan(S,'%s%f%f%f','Whitespace','','TreatAsEmpty',' ','Delimiter','\t')
ans = {{'anna';'bob'},[1.2345;NaN],[NaN;NaN],[67.89;9.7]}
Try the above options with your formatSpec, and just one line, then try it with two lines...
Stephen23
el 8 de Oct. de 2014
The file format uses mixed delimiters (a space and tab), and also uses the space as a placeholder for missing data. I would suggest that the easiest way to deal with this would be to do some pre-processing:
- replace the space between the date and 'K01' with a tab. This would be easy to achieve using regexprep (eg regexprep(str,' K','\tK') ).
- replace the place-holder spaces with nothing... keep those fields empty! This might be the better option, as then you might be able to use (almost) the default settings for textscan.
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!