フィルターのクリア

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

6 ビュー (過去 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 件のコメント
Matt Fig
Matt Fig 2012 年 9 月 12 日
Wow. You aren't kidding!
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 Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by