Reputation: 28907
How can I convert a txt
file (that has n
rows of data) with the following format:
"header1","header2","header3"
1.20,2,3
2.03,3,4
1.05,5,6
8.20,9,4
etc.
into a mat
file that's simply a 2xn
matrix that ignores the first row and first column of the text file and essentially transposes the other columns:
2 3 5 9
3 4 6 4
Upvotes: 0
Views: 1920
Reputation: 11792
There are many ways to do that, but one function in particular allows starting the read at a given row
and column
offset simply in the calling parameter (without having to specify headerlines
or adding *
in format specifier): The function dlmread
In your case, it's a simple matter of calling it with the right parameters:
M = dlmread( 'test.txt' , ',' , 1 , 1 ).' ;
M =
2 3 5 9
3 4 6 4
The nice thing about this function is it returns a double
array directly (useful if you only want to read numbers), instead of a cell array like some other functions.
Note that I transposed the result at the end of the line (using the transpose operator .'
), to get it the way you wanted.
Also note that the last 2 parameters are considered offset (as opposed to start row index). It means a value of 1
specify an offset of 1, so the reading will start at row index 2 (first index is 1).
Upvotes: 3