Reputation: 53806
I've a data file (data.mat) in this format :
load('data.mat')
>> disp(X)
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 0
1 1 1 0
1 1 1 0
1 1 0 0
1 1 0 0
1 1 0 0
>> disp(y)
1
1
1
2
2
2
3
3
3
I'm attempting to create a text datafile(s) that octave can convert to .mat
xvalues.txt :
1, 1, 1, 1
1, 1, 1, 1
1, 1, 1, 1
1, 1, 1, 0
1, 1, 1, 0
1, 1, 1, 0
1, 1, 0, 0
1, 1, 0, 0
1, 1, 0, 0
yvalues.txt :
1
1
1
2
2
2
3
3
3
To instruct Octave to read the xvalues.txt into variable X and yvalues.txt into variable y I use :
X = xvalues.txt
y = yvalues.txt
Is this the idomatic method of reading files in Octave ? These data files will contain 10^6 rows of data at some point, is there a more performant method of loading data files ?
Upvotes: 1
Views: 3393
Reputation: 1123
No need to convert the text files.
You can use the function dlmread()
:
data = dlmread(file, sep, r0, c0)
Read the matrix data from a text file which uses the delimiter
sep
between data values.If
sep
is not defined the separator between fields is determined from the file itself.Given two scalar arguments
r0
andc0
, these define the starting row and column of the data to be read. These values are indexed from zero, such that the first row corresponds to an index of zero.
So simply writing
X = dlmread('xvalues.txt');
y = dlmread('yvalues.txt');
does the job.
Note that Octave can infer the separator (','
in your case) from the file.
Upvotes: 1
Reputation: 3108
In the following code, each column of the xvalues file is read as separate vector, then combined in a matrix X:
[a,b,c,d] = textread("xvalues.txt", "%d %d %d %d", 'delimiter', ',', "endofline", "\n");
X = [a, b, c, d];
[y] = textread("yvalues.txt", "%d", "endofline", "\n");
disp(X);
disp(y);
Check the reference for the textread
here
Upvotes: 1