在Matlab中导入TXT,不同的行格式 [英] Txt import in Matlab, different row formats

查看:169
本文介绍了在Matlab中导入TXT,不同的行格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从txt文件导入变量.该文件包含3个主要部分.

I need to import variables from a txt file. This file has 3 main parts.

A)最初的标题,包含一般信息

A) Initial headlines, containing general information

B)每列中的标题-变量

B) Headlines-Variables, in every column

C)每列中的数值数据

C) Numerical data in every column

如下:

Headlines - Headlines - Headlines - Headlines
Headlines - Headlines - Headlines - Headlines


#    A      |      B              C      |      D        | 
# ----------+----------------------------+---------------|  
#    1      |  0.0000E+00  +  0.0000E+00 |    0.0000     |
#    2/3    |  0.0000E+00 +/- 0.0000E+00 |    0.0000     |
#    4/5    |  0.0000E+00 +/- 0.0000E+00 |    0.0000     |
#    6      |  0.0000E+00  +  0.0000E+00 |    0.0000     |

问题在于,最初的标题每次都在变化,因此我们不能声明要避免的特定行数.

The problem is that the initial headlines are changing every time, so we cant declare a specific number of rows initially to avoid.

如您所见,我们有2种不同的行格式.因此,我们不能为每行写一种特定的格式,并且每一列中的数值数据的数量也在变化.

As you can see we have 2 different row formats. So we cant write a specific format for every line and the number of the numerical data in every column are changing also.

我不能这样做(Data = textscan(fid,'%s%f%s%f%s%f%s%f','headlines',4)

I cant do that (Data=textscan(fid,'%s %f %s %f %s %f %s %f', 'headlines', 4)

我只有两种不同类型的行格式

I have only two different types of row format

如何仅在每一行中导入数字数据.

How can I import only the numerical data in every row.

请帮助

推荐答案

我最喜欢的方法是使用以下神奇命令读取整个文件:

My favourite method is to read in the whole file with this magical command:

buf = textread(文件名,'%s','定界符','\ n');

buf=textread(filename,'%s','delimiter','\n');

,然后对其进行解析.在这种情况下,通过查找初始#似乎很容易检测数据线.

and then to parse it. In this case it seems easy to detect the data lines by looking for an initial #.

这篇关于在Matlab中导入TXT,不同的行格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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