跳过阅读在MATLAB中的标题 [英] skip reading headers in MATLAB

查看:142
本文介绍了跳过阅读在MATLAB中的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这里是我使用的代码。


$ b $ pre $ [c,pathc] = uigetfile({'* .txt'},'选择数据','V:\\ \\数据');
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。 ..............................
................ ..................................................
.............................................. ....................

和所以

解决方案

使用 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 功能将它们转换回来。 / p>

I had a similar question. but what i am trying now is to read files in .txt format into MATLAB. My problem is with the headers. Many times due to errors the system rewrites the headers in the middle of file and then MATLAB cannot read the file. IS there a way to skip it? I know i can skip reading some characters if i know what the character is.

here is the code i am using.

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

this way i let the user pick the file. My files are huge typically [ 86400 125 ] so naturally it has 125 header fields or more depends on files.

Thanks

Because the files are so big i cannot copy , but its in format like

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

and so on

解决方案

With DLMREAD you can read only numeric data. It will not read date and time, as your first two columns contain. If other data are all numeric you can tell DLMREAD to skip first row and 2 columns on the right:

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

To import also day and time you can use IMPORTDATA instead of 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;

The date and time will be converted to serial numbers. You can convert them back with DATESTR function.

这篇关于跳过阅读在MATLAB中的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆