在MATLAB中跳过阅读标题

时间:2010-04-06 20:23:48

标签: matlab file-io

我有一个类似的问题。但我现在正在尝试的是将.txt格式的文件读入MATLAB。我的问题是标题。很多时候由于错误,系统会重写文件中间的标题,然后MATLAB无法读取文件。有没有办法跳过它?我知道如果我知道这个角色是什么,我可以跳过阅读一些角色。

这是我正在使用的代码。

[c,pathc]=uigetfile({'*.txt'},'Select the data','V:\data');     
file=[pathc c];    
data= dlmread(file, ',', 1,4);    

这样我让用户选择文件。我的档案很大[86400 125] 所以很自然它有125个标题字段或更多取决于文件。

由于

因为文件太大我无法复制,但其格式如

  day      time    col1    col2 col3 col4 ...............................
  2/3/2010  0:10    3.4    4.5   5.6  4.4 ...............................   
..................................................................    
..................................................................   

等等

2 个答案:

答案 0 :(得分:3)

使用DLMREAD,您只能读取数字数据。它不会读取日期和时间,因为前两列包含。如果其他数据都是数字,您可以告诉DLMREAD跳过右边的第一行和第二列:

data = dlmread(file, ' ', 1,2); 

要导入日期和时间,您可以使用IMPORTDATA代替DLMREAD:

A = importdata(file, ' ', 1);
dt = datenum(A.textdata(2:end,1),'mm/dd/yyyy');
tm = datenum(A.textdata(2:end,2),'HH:MM');
data = A.data;

日期和时间将转换为序列号。您可以使用DATESTR函数将其转换回来。

答案 1 :(得分:2)

事实证明你仍然可以使用文本扫描。除了你把所有东西都读成字符串。然后,您尝试转换为double。 'str2double'为字符串返回NaN,并且由于标题都是字符串,因此您可以将标题行标识为包含所有NaN的行。

例如:

%# find and open file
[c,pathc]=uigetfile({'*.txt'},'Select the data','V:\data'); 
file=[pathc c];
fid = fopen(file);

%# read all text
strData = textscan(fid,'%s%s%s%s%s%s','Delimiter',','); 

%# close the file again
fclose(fid);

%# catenate, b/c textscan returns a column of cells for each column in the data
strData = cat(2,strData{:}); 

%# convert cols 3:6 to double
doubleData = str2double(strData(:,3:end));

%# find header rows. headerRows is a logical array
headerRowsL = all(isnan(doubleData),2);

%# since I guess you know what the headers are, you can just remove the header rows
dateAndTimeCell = strData(~headerRowsL,1:2);
dataArray = doubleData(~headerRowsL,:);

%# and you're ready to start working with your data