MATLAB: How to split data text file with unequal elements in the row into several files

MATLABread data from text filesplit data fileunequal elements in the row

How to split data text file with unequal elements in the row into several files? the parts in file are separated by blank line. The last value in the first non blank line of the chain is a parameter for the name of the files that should be obtained. (Very first line is blank)
start ====================================================================
169.454910 2.43834758 -3 80 167.000000
168.831055 1.62695873 -3 1360
168.169083 1.72711039 -3 2640
168.336090 2.58010888 -3 3920
169.114761 3.06297684 -3 5200
170.748550 2.64059544 -2 80 168.000000
168.722565 0.179684386 -2 1360
166.820984 0.642302752 -2 2640
167.385132 3.19096994 -2 3920
169.737167 4.61249638 -2 5200
172.051773 2.84667945 -1 80 169.000000
168.475510 -1.35426331 -1 1360
165.393097 -0.431597352 -1 2640
166.334351 3.77082968 -1 3920
170.268417 6.19379234 -1 5200
173.364777 3.05668116 0 80 170.000000
168.078079 -2.97351408 0 1360
163.881104 -1.48867857 0 2640
165.183487 4.32388353 0 3920
170.708206 7.80115700 0 5200
=========================================================================

Best Answer

With just one textscan call. The test file is attached (you did not provide us with a sample file).
opt = {'CollectOutput',true};
[fid,msg] = fopen('test.txt','rt');
assert(fid>=3,msg)
C = textscan(fid,'%f%f%f%f%f',opt{:});
fclose(fid);
M = C{1};
D = diff([isnan(M(:,5));false]);
E = find(D<0);
B = find(D>0);
for k = 1:numel(B)
F = sprintf('new %d.csv',M(B(k),5));
csvwrite(F,M(B(k):E(k),1:4))
end