reading a data file with lines of different length

I am trying to translate a program written in Pascal into Matlab. I am blocked with the opening and reading of a data file which is attached here.
FILE
2
9
5.8688000000000000E+0000 3.7543377578475808E+0000 2.3741040000000000E+0001
4.6882400000000000E-0002 6.8480000000000000E-0002 1.0378390189135331E-0001 7.837578058E-01
4.3095200000000000E-0001 4.6940000000000000E-0001 1.5901029887692748E-0001
5.4136920610605448E-0002 8.6036251581137500E-0002 1.7313773960029888E-0001
1.4000000000000000E+0002
The problem is that the length of each line is different, and the extension is not txt or dat (and I cannot change it).
How can I save the variables so I can use them afterwards? thanks

Answers (1)

f = fopen('yourfile.txt');
c = textscan(f,repmat('%f ',1,4));
fclose(f);
m = cellfun(@numel,c);
k = arrayfun(@(x,y)[x{1};nan(max(m)-y,1)],c,m,'un',0);
out = cat(2,k{:});

Products

Asked:

s
s
on 26 Jun 2012

Community Treasure Hunt

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

Start Hunting!