在Matlab中读取txt文件

在Matlab中读取txt文件

本文介绍了在Matlab中读取txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

读取包含10列和2行标题的 txt 文件时遇到问题,但是问题是在文件中间出现了相同的标题几次, textread()不起作用。这是我的档案例子:
$ b $ h3 file.txt

$ $ $ $ $ $ $ $ headerline1 aaaa
headerline2 111 123
20/12/2000 name1 name2 name3 ... name8 0
21/12/2000 name1 name2 name3 ... name8 0
22/12/2000 name1 name2 name3 ... name8 0
headerline1 aaaa
headerline2 111 123
25/12/2000 name1 name2 name3 ... name8 0
27/12/2000 name1 name2 name3。 .. name8 0
...

这是我的代码我试过了: p>

  [日期,名称1,名称2,名称3,名称4,名称5,名称6,名称7,名称8,状态] = ... 
)textread('file.txt','%s%s%s%s%s%s%s%s%s%d','headerlines',2);

恰好在重复标题的行处给出错误。你有什么想法,我怎么能避免这些标题,并阅读完整的文件?问题是我有几百这些类型的文件,所以我不能删除每一次手动。

感谢您的帮助。

textscan 逐行读取文件,将整行作为一个字符串。然后删除标题,然后处理其余的内容。

这是一个例子:

 %#将整个文件读到一个临时单元格数组
fid = fopen(filename,'rt');
tmp = textscan(fid,'%s','Delimiter','\\\
');
fclose(fid);

%#删除标题行
tmp = tmp {1};
idx = cellfun(@(x)strcmp(x(1:10),'headerline'),tmp);
tmp(idx)= [];

%#拆分并连接其余的
result = regexp(tmp,'','split');
result = cat(1,result {:});

#删除临时数组(如果你想的话)
清除tmp


I have a trouble reading the txt file, which contains 10 columns and 2 lines of header, but the problem is that in the middle of the file the same header appears several times and textread() doesnt function. That's my file example:

file.txt

headerline1 aaaa
headerline2 111 123
20/12/2000 name1 name2 name3... name8 0
21/12/2000 name1 name2 name3... name8 0
22/12/2000 name1 name2 name3... name8 0
headerline1 aaaa
headerline2 111 123
25/12/2000 name1 name2 name3... name8 0
27/12/2000 name1 name2 name3... name8 0
...

and this is my code I tried:

[date, name1, name2, name3, name4, name5, name6, name7, name8, status] = ...
    textread('file.txt', '%s %s %s %s %s %s %s %s %s %d', 'headerlines',2);

It gives the error exactly at the row with the repeated header. Do you have any ideas how could I avoid those headers and read the complete file? The problem is that I have hundreds of these types of files, so I cant delete each time manually.

Thanks for help.

解决方案

You can first read the file line by line with textscan taking the whole line as a string. Then remove the headerlines, and process the rest

Here is an example:

%# read the whole file to a temporary cell array
fid = fopen(filename,'rt');
tmp = textscan(fid,'%s','Delimiter','\n');
fclose(fid);

%# remove the lines starting with headerline
tmp = tmp{1};
idx = cellfun(@(x) strcmp(x(1:10),'headerline'), tmp);
tmp(idx) = [];

%# split and concatenate the rest
result = regexp(tmp,' ','split');
result = cat(1,result{:});

%# delete temporary array (if you want)
clear tmp

这篇关于在Matlab中读取txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:09