MATLAB: Importing data with unequal number of column

data import

Hi all,
Firstly, I apologise if the title is not describing what I am going to ask here. Please feel free to change the title of this post.
I have the following dataset save as txt file and I intend to load this into Matlab. The matrix is suppose to be a [3 x 178], how to do this?
Hit:
The first row is started by '10864.0', the second row is by '10864.5' and the third is by '10865.0'.
I have attached the original data.txt to this post.

Best Answer

This is very simple and efficient using fscanf:
[fid,msg] = fopen('Data.txt','rt');
assert(fid>=3,msg)
mat = fscanf(fid,'%f',[179,3]).';
fclose(fid);
or using fileread and sscanf:
str = fileread('Data.txt');
mat = sscanf(str,'%f',[179,3]).'
Giving:
>> size(mat)
ans =
3 179
>> mat(:,1:11) % Look at just the first few columns:
ans =
10864 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069
10865 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069
10865 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069 3.3069
Related Question