필터 지우기
필터 지우기

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

조회 수: 7 (최근 30일)
Mathew
Mathew 2012년 9월 10일
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
Oleg Komarov
Oleg Komarov 2012년 9월 10일
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 2012년 9월 11일
this is the file to be imported:
The first empty lines are some 60 rows down.

댓글을 달려면 로그인하십시오.

답변 (2개)

per isakson
per isakson 2012년 9월 12일
편집: per isakson 2012년 9월 12일
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
per isakson
per isakson 2012년 9월 12일
편집: per isakson 2012년 9월 12일
Yes, and it doesn't make it pass row 60, which however is not an empty row.

댓글을 달려면 로그인하십시오.


Tom
Tom 2012년 9월 12일
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,:)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by