[英]Txt import in Matlab, different row formats
I need to import variables from a txt file. 我需要从txt文件导入变量。 This file has 3 main parts.
该文件包含3个主要部分。
A) Initial headlines, containing general information A)初始标题,包含一般信息
B) Headlines-Variables, in every column B)每列的标题-变量
C) Numerical data in every column C)每列中的数值数据
As below: 如下:
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. 问题在于,最初的标题每次都在变化,因此我们最初不能声明特定数量的行来避免。
As you can see we have 2 different row formats. 如您所见,我们有2种不同的行格式。 So we cant write a specific format for every line and the number of the numerical data in every column are changing also.
因此,我们不能为每一行写一种特定的格式,并且每一列中的数值数据的数量也在变化。
I cant do that (Data=textscan(fid,'%s %f %s %f %s %f %s %f', 'headlines', 4) 我不能这样做(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. 如何仅导入每一行中的数字数据。
Please HELP 请帮忙
You can apply textscan
line by line instead of to the file as a whole. 您可以逐行而不是整个应用
textscan
。 For example, based on the example you gave (and assuming you have written a function to determine the data format from the top lines): 例如,根据您给出的示例(并假设您已经编写了一个函数来确定最上面几行的数据格式):
fileID = fopen(fileName);
blockLine = 0;
while ~feof(fileID)
currLine = fgetl(fileID);
% Check if we've reached the datablock
if strcmpi(currLine(1),'#')
blockLine = blockLine + 1;
end
% Use first line of datablock to determine textscan format
if blockLine == 1
textFormat = [insert format determination function];
elseif blockLine > 2
% Ignoring second line (dashes only)
lineData = textscan(currLine,textFormat);
[insert code to distribute data to larger variables]
end
end
fclose(fileID);
My favourite method is to read in the whole file with this magical command: 我最喜欢的方法是使用以下神奇命令读取整个文件:
buf=textread(filename,'%s','delimiter','\\n');
buf = textread(文件名,'%s','定界符','\\ n');
and then to parse it. 然后解析它。 In this case it seems easy to detect the data lines by looking for an initial #.
在这种情况下,通过查找初始#似乎很容易检测到数据线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.