Filter löschen
Filter löschen

reading data using textscan: how to record empty rows with muliple columns?

6 Ansichten (letzte 30 Tage)
Mathew
Mathew am 10 Sep. 2012
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 Kommentare
Oleg Komarov
Oleg Komarov am 10 Sep. 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?
Mathew
Mathew am 11 Sep. 2012
this is the file to be imported:
The first empty lines are some 60 rows down.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

per isakson
per isakson am 12 Sep. 2012
Bearbeitet: per isakson am 12 Sep. 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 ---
  1. Empty values are not the problem. textscan returns NaN for empty numerical, %f, and '' for empty strings, %s.
  2. The problem is because not all rows in the file have the same format
  3. set 'ReturnOnError' , false
  4. It seems as if "\tCalm" needs to be replaced by "Calm\t"
  5. Fix the program that wrote the file or
  6. Read the complete file as text, replace "\tCalm" by "Calm\t" and parse the string buffer with textscan
  2 Kommentare
per isakson
per isakson am 12 Sep. 2012
Bearbeitet: per isakson am 12 Sep. 2012
Yes, and it doesn't make it pass row 60, which however is not an empty row.

Melden Sie sich an, um zu kommentieren.


Tom
Tom am 12 Sep. 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,:)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by