How does one input a large amount of data that has a different number of columns per row?

Hi, I am trying to load in a few thousand files using textscan that are in the following format:
3245
0,1,2,3,4,......500
0.23,0.35,0.41,0.57,1.32
0.10,0.83,0.92,1.45
0.66,0.94,1.75,2.35,2.51,3.90
0.13,0.71
I only want to load starting at the 3rd row (i.e. at 0.23). However, starting at the third row, there are actually 500 more rows and a variable number of columns (on the order of a few hundred). (I don't need to load in the first two lines). But I can't figure out how to do this. I can't input them row by row, because there are 500 of them, and a few thousand files. I want to be able to access each number separately of course (say data(2,3)=0.92), and to fill in the extra spaces with NaN or something. Can someone please help me with this?
Kyla

 Accepted Answer

textscan cannot parse rows with variable number of columns. AFAIK: there is no function in Matlab that can do that.
There are many approaches to read the file. Two of them:
  1. one line at a time with fgetl
  2. read each line as a string with textscan and parse in a second step
.
--- textscan CAN parse rows with variable number of columns ---
I was wrong. This code parses the first NCOL columns, where NCOL is the number of columns of the shortest row.
fid = fopen('h:\m\cssm\ascii_with_variable_row_length.txt');
cac = textscan( fid, '%f%f%*[^\n]', 'CollectOutput', true );
fclose( fid );
cac{1}
prints
ans =
17 24
23 5
4 6
10 12
11 18
where 'h:\m\cssm\ascii_with_variable_row_length.txt' contains
17 24 1 8 15
23 5 7
4 6 13 20 22
10 12
11 18 25 2 9

More Answers (0)

Categories

Asked:

on 22 Sep 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!