从 Matlab 读取文本文件时查找特定字符串所在的行

发布于 2024-12-17 07:43:45 字数 894 浏览 3 评论 0原文

我有一个文本文件,其中包含字符串周围的连续整数值。该文件看起来像这样:

This is the set for x = 100
---------------------------

For y=COLUMN 1 we have
1232
3ff3
4a45
23d4
5323
...
...
END of COLUMN 1 meas

For y=COLUMN 2 we have
1232
3c43
4545
2d24
5a23
...
...
END of COLUMN 2 meas

This is the set for x = 200
---------------------------

For y=COLUMN 1 we have
2b23
1232
d387
6f74
4c47
...
...
END of COLUMN 1 meas

For y=COLUMN 2 we have
354d
a546
3c63
5a46
a349
...
...
END of COLUMN 2 meas

This is the set for x = 530
---------------------------
..........
..........

我想做的是将字符串之间的值存储到单独的数组中。也就是说,从“For y=COLUMN 1 we have”到“END of COLUMN 1 meas”将存储到ArrayA中,从“For y=COLUMN 2 we have”到“END of COLUMN 2 meas”存储到ArrayB中,等等 之后,

我需要找到“x”的所有值并将它们存储到名为 ArrayX 的字符串数组中。也就是说,它应该是这样的:

ArrayX =

'x=100'    'x=200'    'x=501'

如果有人可以提供帮助,我将非常感激。

I have a text file that has consequtive integer values around strings. This file looks like that:

This is the set for x = 100
---------------------------

For y=COLUMN 1 we have
1232
3ff3
4a45
23d4
5323
...
...
END of COLUMN 1 meas

For y=COLUMN 2 we have
1232
3c43
4545
2d24
5a23
...
...
END of COLUMN 2 meas

This is the set for x = 200
---------------------------

For y=COLUMN 1 we have
2b23
1232
d387
6f74
4c47
...
...
END of COLUMN 1 meas

For y=COLUMN 2 we have
354d
a546
3c63
5a46
a349
...
...
END of COLUMN 2 meas

This is the set for x = 530
---------------------------
..........
..........

What I would like to do is to strore the values in between the strings into seperate arrays. That is, from 'For y=COLUMN 1 we have' to 'END of COLUMN 1 meas' will be strored in to ArrayA, from 'For y=COLUMN 2 we have' to 'END of COLUMN 2 meas' into ArrayB, etc.

After that, I need to find all the values fo 'x' and store them into a string array named ArrayX. That is, this should be look like that:

ArrayX =

'x=100'    'x=200'    'x=501'

If anyone can help, I will really appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断舍离 2024-12-24 07:43:45

我不确定您是否仍然需要此信息,但我有同样的问题并找到了此解决方案;

formatSpec = 'This is the set for x = %s\n---------------------------\n\nFor y=COLUMN %s we have\n%s\n%s\n%s\n%s\n%s\nEND of COLUMN 1 meas\n\nFor y=COLUMN 2 we have\n%s\n%s\n%s\n%s\n%s\nEND of COLUMN 2 meas\n\n';
fileID = fopen('new.txt','r');
A = fscanf(fileID,formatSpec,[4 Inf]);
A=A';

现在我有一个包含所有需要的符号的大矩阵。我获取了你的数据并得到了这个:

A =

1001
1232
3ff3
4a45
23d4
5323
1232
3c43
4545
2d24
5a23
2001
2b23
1232
d387
6f74
4c47
354d
a546
3c63
5a46
a349
530 

所以最后一步是将这个矩阵划分为几个。例如,对于您的ArrayX

ArrayX = A(1:11:end,1:3)
ArrayX =

100
200
530

我找不到一些优雅的方法来按照您想要的列数划分A,并且只是使用循环以相同的方式完成。

I'm not sure you still need this information, but I have the same problem and find this solution for this;

formatSpec = 'This is the set for x = %s\n---------------------------\n\nFor y=COLUMN %s we have\n%s\n%s\n%s\n%s\n%s\nEND of COLUMN 1 meas\n\nFor y=COLUMN 2 we have\n%s\n%s\n%s\n%s\n%s\nEND of COLUMN 2 meas\n\n';
fileID = fopen('new.txt','r');
A = fscanf(fileID,formatSpec,[4 Inf]);
A=A';

Now I have big matrix with all needed symbols. I took your data and get this:

A =

1001
1232
3ff3
4a45
23d4
5323
1232
3c43
4545
2d24
5a23
2001
2b23
1232
d387
6f74
4c47
354d
a546
3c63
5a46
a349
530 

So the last step is to divide this matrix at several. For example for your ArrayX:

ArrayX = A(1:11:end,1:3)
ArrayX =

100
200
530

I can't find some elegant way to divide A at number of columns as you wanted and just done the same way using loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文