I have a data set that I would like to store and be able to load in Octave
18.0 8 307.0 130.0 3504. 12.0 70 1 "chevrolet chevelle malibu"
15.0 8 350.0 165.0 3693. 11.5 70 1 "buick skylark 320"
18.0 8 318.0 150.0 3436. 11.0 70 1 "plymouth satellite"
16.0 8 304.0 150.0 3433. 12.0 70 1 "amc rebel sst"
17.0 8 302.0 140.0 3449. 10.5 70 1 "ford torino"
15.0 8 429.0 198.0 4341. 10.0 70 1 "ford galaxie 500"
14.0 8 454.0 220.0 4354. 9.0 70 1 "chevrolet impala"
14.0 8 440.0 215.0 4312. 8.5 70 1 "plymouth fury iii"
14.0 8 455.0 225.0 4425. 10.0 70 1 "pontiac catalina"
15.0 8 390.0 190.0 3850. 8.5 70 1 "amc ambassador dpl"
It does not work immediately when I try to use:
data = load('auto.txt')
Is there a way to load from a text files with the given format or do I need to convert it to e.g
18.0,8,307.0,130.0,3504.0,12.0,70,1
...
EDIT: Deleting the last row and fixing the 'half' number e.g. 3504. -> 3504.0 and then used:
data = load('-ascii','autocleaned.txt');
Loaded the data as wanted in to a matrix in Octave.
If the final string field is removed from each line, the file can be read with:
Alternatively the whole file could read in as one column and reshaped.
I haven't figured out how to read both the numeric fields and the string field. For that I've had to fall back on Python with more general purpose file reading tools.
Here is a Python script that reads the file, creates a
numpy
structured array, writes that to a.mat
file, whichOctave
can then read:In
Octave
this could read withNewer Octave (3.8) has an
importdata
function. It handles the original data file without any extra arguments. It returns a structure with 2 fieldsx.data
is a(10,11)
matrix.x.data(:,1:8)
is the desire numerical data.x.data(:,9:11)
is a mix ofNA
and random numbers. TheNA
stand in for the words at the end of the lines.x.textdata
is a(24,1)
cell with those words. The quoted string s could be reassembled from those words, using theNA
and quotes to determine how many words belong to which line.To read the numeric data it uses
dlmread
. Since the rest ofimportdata
is written inOctave
, it could be used as the starting point for a custom function that handles the string data properly.load
is usually meant for loading octave and matlab binary files but can be used for loading textual data like yours. You can load your data using the"-ascii"
option but you'd have to reformat your file slightly before putting it intoload
even with the"-ascii"
option enabled. Use a consistent column separator ie. just a tab or a comma, use full numbers not3850.
and don't use strings.Then you can do something like this to get it to work
https://octave.org/doc/v4.0.0/Simple-File-I_002fO.html
Try this,