Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and parse text file in octave/matlab

Tags:

matlab

octave

I am trying to read in some values from a file in an octave program (I suspect matlab is similar), but not sure how to do it.

I have input file in the form:

x y
A B C
a_11 ...  a_1n
a_21 ..   a_2n
...
a_m1 ...  a_mn

Where x,y are doubles, A, B, C are integers, and a_11...a_mn is a matrix.

I saw examples of how to read in just the matrix, but how can I read mixed stuff like this?

like image 993
Andriy Drozdyuk Avatar asked Nov 23 '11 07:11

Andriy Drozdyuk


1 Answers

In my opinion this is not a good way of storing data. But octave offers the functionality to read this as well with dlmread:

data = dlmread (file, sep, r0, c0)
data = dlmread (file, sep, range)

If you have this text file test.csv:

1 2
1.1 2.2 3.3 4.4
1 2 3
4 5 6
7 8 9

You can read in your data like this:

integers = dlmread('test.csv', '', [0 0 0 1]);
floats   = dlmread('test.csv', '', [1 0 1 3]);
matrix   = dlmread('test.csv', '', 2, 0);
like image 112
Woltan Avatar answered Sep 21 '22 10:09

Woltan