reading data using textscan: how to record empty rows with muliple columns?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
hi I'm trying to import a text file that has 19 columns, is quite long, and has several rows that are empty. I want these empty rows to be recorded. The columns are separated by tabs. I have this so far:
B=textscan(fid,'%s %f %f %f %f %s %f %s %f %f %f %s %f %f %f %f %s %f %f','\t');
which works extremely well until it hits the rows with empty cells at which points it stops. Can anyone suggest something to add to the above line to force textscan to continue through the data whilst recording the empty cells?
2 comentarios
Oleg Komarov
el 10 de Sept. de 2012
The syntax you're using is incorrect, you missed 'Delimiter','\t'.
Alos, can you post few lines with an empty line or upload part of e file somewhere and drop the link here?
Respuestas (2)
per isakson
el 12 de Sept. de 2012
Editada: per isakson
el 12 de Sept. de 2012
It's sure a pain to get the format string right. There are no space (char(32)) in the data file. I seriously doubt that your textscan expression ever worked.
This code reads the file up to, but not including "Calm" in row 60. First appearance of "Calm" in the file. There is one tab to many between "11:08" and "Calm"
file_spec = 'h:\m\cssm\cssm_a.txt';
fid = fopen( file_spec, 'r' );
cup = onCleanup( @() fclose(fid) );
% 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
frm = '%s%f%f%f%f%f%s%f%s%f%f%f%s%f%f%f%f%f%s%f%f';
cac = textscan( fid, frm ...
, 'Delimiter' , '\t' ...
... , 'EmptyValue' , NaN ...
);
clear('cup')
.
--- Comment ---
As default, textscan (R2012a) does not return a message when reading terminates early: "If true, textscan terminates without an error and returns all fields read." Thus set ReturnOnError
cac = textscan( fid, frm ...
, 'Delimiter' , '\t' ...
, 'ReturnOnError' , false ...
);
--- Conclusions ---
- Empty values are not the problem. textscan returns NaN for empty numerical, %f, and '' for empty strings, %s.
- The problem is because not all rows in the file have the same format
- set 'ReturnOnError' , false
- It seems as if "\tCalm" needs to be replaced by "Calm\t"
- Fix the program that wrote the file or
- Read the complete file as text, replace "\tCalm" by "Calm\t" and parse the string buffer with textscan
2 comentarios
per isakson
el 12 de Sept. de 2012
Editada: per isakson
el 12 de Sept. de 2012
Yes, and it doesn't make it pass row 60, which however is not an empty row.
Tom
el 12 de Sept. de 2012
This is probably not too robust, and the numbers are still strings not numerics, but it picks up the empty spaces:
str=fileread('a.txt');
C=regexp(str,'\t|\n','split');
Data=reshape(C,21,[])';
Data(:,end)=deblank(Data(:,end));
%test:
Data(67:75,:)
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!