How do I read a delimited file with strings/numbers with Octave?

rs028 picture rs028 · Mar 14, 2011 · Viewed 34.2k times · Source

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?

Answer

Jordan picture Jordan · Jun 12, 2012

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 )