加载包含数字和字母的文本文件
我有一个如下所示的文本文件:(
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用 fopen、fscanf 和 fclose。对于仅包含字母的行(如
'%s\t%s\t%s'
)和仅包含数字的行(如'%g\ t%g\t%g'
)。您可以使用单个 fprintf 调用读取相同结构的行。示例文件(data.txt):
假设我们事先知道该文件包含 3 列,2 行开头有字符:
带有字符的行将在
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):
Suppose that we know in advance that the file contains 3 columns, and 2 lines with characters at the beginning:
The lines with the characters will be in
x'
, and the lines with numbers will be contained byy'
.