MATLAB: Reading text file with irregular format

text file

I have a text file with this format:
1 2016 1 0
25.25 45.45
25.25 15.05
25.25 5.45
25.25 30.11
25.25 42
2 2016 1 1
5.25 -6.45
5.25 -6.45
5.25 -0.5
5.25 -1.35
5.25 2.15
3 2016 1 2
2.25 -15.45
2.25 35.45
2.25 -5.45
2.25 15.45
2.25 15.45
How can I read it in Matlab?

Best Answer

It's still unclear how you'd like the final dataset to be arranged but this should get you there.
I read in your sample data stored in a file 'test.txt'. It fills empty elements with '0'. Then I identify which rows have all 0s in columns beyond column 2.
% Read the full file
t = dlmread('test.txt');
% Identify the rows with > 2 columns
% This method assumes that rows with >2 columns don't contain all
% zeros in columns 3:end.
twoColIdx = all(t(:,[3:end]) == 0, 2);
% Data with >2 columns
t(~twoColIdx, :)
% Data with 2 columns
t(twoColIdx, [1,2])