matlab - 如何在MATLAB中跳过数据之间的某些行时从文本中提取两列数据?
问题描述
我得到我的数据如下(参考大数据)。我需要提取文本文件第 19 行之后的数据行。为此,我可以使用以下代码。
filename= ['f.rpt'];
fid = fopen(filename);
A = textscan(fid, '%f %f','HeaderLines',19) ;
b = A{2};
fclose(fid);
但是,这只会读取和提取数据,直到包含
Field Output reported at nodes for part: PART-2-1
Node CSMAXSCRT
Label @Loc 1
---------------------------------
我想跳过那些不属于两列数据的行,并从下面的行中提取数据。问题是,我不知道如何跳过这些行并仅提取两列数据。我要跳过的间歇性数据的位置不是恒定的,并且会随着不同的输出而变化。有没有办法做到这一点?
********************************************************************************
Field Output Report
Source 1
---------
ODB: ffffff
Step: Step-1
Frame: Increment 7600: Step Time = 6.0000E-011
Loc 1 : Nodal values from source 1
Output sorted by column "CSMAXSCRT General_Contact_Domain".
Field Output reported at nodes for part: PART-1-1
Node CSMAXSCRT
Label @Loc 1
---------------------------------
1 1.
2 1.
3 1.
4 1.
456 1.
455 1.
454 1.
453 1.
452 1.
451 1.
450 1.
12 1.
13 1.
14 1.
15 1.
16 1.
17 1.
18 1.
19 1.
20 1.
21 1.
Field Output reported at nodes for part: PART-2-1
Node CSMAXSCRT
Label @Loc 1
---------------------------------
1 1.
2 1.
3 1.
4 1.
5 1.
6 1.
7 1.
8 1.
9 1.
10 1.
11 1.
12 1.
13 1.
14 1.
15 1.
16 1.
17 1.
18 1.
19 1.
20 1.
21 1.
22 1.
23 1.
Field Output reported at nodes for part: PART-3-1
Node CSMAXSCRT
Label @Loc 1
---------------------------------
1 1.
2 1.
3 1.
4 1.
5 1.
6 1.
7 1.
8 1.
9 1.
10 1.
11 1.
12 1.
13 1.
14 1.
15 1.
16 1.
17 1.
18 1.
19 1.
20 1.
21 1.
22 1.
23 1.
解决方案
使用该textscan
方法时,可以恢复扫描。我在以下解决方案中加入了这种可能性。我假设您处理的是数字数据,因此我直接相应地解析了元胞数组输出textscan
。如果不需要,则需要修改附加的数据。
% Read in whole text.
text = fileread('f.txt');
% Initialize result array.
C = [];
% Initialize absolute cursor position.
absPos = 0;
% While absolute cursor position hasn't reached EOF.
while (absPos < numel(text))
% First read -> Skip 19 header lines.
if (absPos == 0)
[data, relPos] = textscan(text, '%f %f', 'HeaderLines', 19);
absPos = absPos + relPos;
% Every further read -> Skip 5 intermediate header lines.
else
[data, relPos] = textscan(text((absPos+1):end), '%f %f', 'HeaderLines', 5);
absPos = absPos + relPos;
end
% Append data.
C = [C; [data{:}]];
end
由于它的长度,我不想将输出复制粘贴到这里。请你自己看看。
推荐阅读
- angular - 如何使用 videogular2 以全屏方式默认制作视频?
- tensorflow - 将 Convolutional2D dim_ordering 参数迁移到 tf.keras 中的 Conv2D
- flutter - 在 Flutter 中使用 json 数据自动完成建议和搜索
- visual-studio - VS2019 使用 runas 作为不同用户进行调试,但不会在断点处停止
- android - 偏好的保存位置始终为空
- c# - Google 无法呈现和索引我的服务器端 Blazor 组件
- sap-cloud-sdk - 需要指导 - 使用 ODATA 服务的方法
- java - Apache Camel - 仅当队列大小大于
- excel - 在excel中使用vba循环TEXTJOIN以获得多个输出
- centos7 - 在 centos 7 中安装后的 LibreOffice 错误