如何使用Octave读取带字符串/数字的分隔文件? [英] How do I read a delimited file with strings/numbers with Octave?

查看:202
本文介绍了如何使用Octave读取带字符串/数字的分隔文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Octave读取包含数字和字符串的文本文件。文件格式如下:

I am trying to read a text file containing digits and strings using Octave. The file format is something like this:


ABC

a 10 100¥
b 20 200

c 30 300

d 40 400¥$ b $ 50 500¥
A B C
a 10 100
b 20 200
c 30 300
d 40 400
e 50 500

但分隔符可以是空格,制表符,逗号或分号。如果分隔符是空格/制表符,则textread函数可以正常工作:

but the delimiter can be space, tab, comma or semicolon. The textread function works fine if the delimiter is space/tab:

[A,B,C] = textread('test.dat', '%s%d%d','headerlines',1)

但是,如果分隔符是逗号/分号,则它不起作用。我尝试使用dklmread:

However it does not work if delimiter is comma/semicolon. I tried to use dklmread:

dlmread('test.dat',';',1,0)

但它不起作用,因为第一列是一个字符串。
基本上,使用textread我无法指定分隔符,而使用dlmread我无法指定第一列的格式。至少在Octave中没有这些功能的版本。有没有人以前遇到过这个问题?

but it does not work because the first column is a string. Basically, with textread I can't specify the delimiter and with dlmread I can't specify the format of the first column. Not with the versions of these functions in Octave, at least. Has anybody ever had this problem before?

推荐答案

textread 允许您指定分隔符 - 它尊重 strread 。以下代码适用于我:

textread allows you to specify the delimiter-- it honors the property arguments of strread. The following code worked for me:

[A,B,C] = textread( 'test.dat', '%s %d %d' ,'delimiter' , ',' ,1 )

这篇关于如何使用Octave读取带字符串/数字的分隔文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆