加载包含数字和字母的文本文件

发布于 2024-12-17 04:48:00 字数 215 浏览 2 评论 0原文

我有一个如下所示的文本文件:(

A    B    C
1    2    3

这只是我实际拥有的一个最小示例。我的实际文件很大并且行数各不相同。)

我想将此文件加载到 Octave 中。但是,该文件包含字母,而不仅仅是数字。当我尝试应用加载函数时,出现错误,我猜这是因为加载函数只接受数字。 我应该使用什么函数?

I have a text file that looks like so:

A    B    C
1    2    3

(This is just a minimal example of what I actually have. My actual files are HUGE and vary in number of rows.)

I would like to load in this file into Octave. However, the file contains letters, rather than just numbers. When I'm trying to apply the load function, I get errors, and I guess this is because the load function only accepts numbers. What function should I use instead?

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

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

发布评论

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

评论(1

成熟稳重的好男人 2024-12-24 04:48:00

调用 fopen、fscanf 和 fclose。对于仅包含字母的行(如 '%s\t%s\t%s')和仅包含数字的行(如 '%g\ t%g\t%g')。您可以使用单个 fprintf 调用读取相同结构的行。

示例文件(data.txt):

A        B        C
D        E        F
1        2        3
4        5        6
7        8        9
10       11       12

假设我们事先知道该文件包含 3 列,2 行开头有字符:

fid = fopen('data.txt', 'r');
[x, nx] = fscanf(fid, '%s\t%s\t%s', [3, 2]);
[y, ny] = fscanf(fid, '%g\t%g\t%g', [3, Inf]);
fclose(fid);

带有字符的行将在 x' 中,并且带有数字的行将包含在 y' 中。

Call fopen, fscanf, and fclose. The format string must be different for lines containing only letters (like '%s\t%s\t%s'), and for those which contain only numbers (like '%g\t%g\t%g'). You can read lines of identical structure with a single fprintf call.

Example file (data.txt):

A        B        C
D        E        F
1        2        3
4        5        6
7        8        9
10       11       12

Suppose that we know in advance that the file contains 3 columns, and 2 lines with characters at the beginning:

fid = fopen('data.txt', 'r');
[x, nx] = fscanf(fid, '%s\t%s\t%s', [3, 2]);
[y, ny] = fscanf(fid, '%g\t%g\t%g', [3, Inf]);
fclose(fid);

The lines with the characters will be in x', and the lines with numbers will be contained by y'.

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