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

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

问题描述

我正在尝试使用 Octave 读取包含数字和字符串的文本文件.文件格式是这样的:

<块引用>A B C
一个 10 100
b 20 200
c 30 300
d 40 400
e 50 500

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

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

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

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

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

解决方案

textread 允许您指定分隔符 - 它遵循 strread<的属性参数/a>.以下代码对我有用:

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

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

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

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)

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

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

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 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天全站免登陆