MATLAB: Read data from delimited text file

dlmreadMATLABnestedloops

Hi,
I have a delimited text file that contains 8 rows and 4 columns and this is repeated for 'n' sets. I need to copy the 4th column value of each rows and make a new variable.
There will be a total of 8 variables with dimension (n,1).
I can use dlm read to read the file. But I am having trouble creating nested loops to achieve the goal.
I have attached the text file for reference. What would be the best way to tackle the problem ?

Best Answer

This is one of the more difficult files I have managed to import!
See if this does what you want:
fidi = fopen('water.txt');
for k = 1:3
hdr{k,:} = fgets(fidi); % Read & Store Header Lines
end
k1 = 1;
while ~feof(fidi)
sechdr(k1,:) = textscan(fidi, '%f%f',1);
if isempty(sechdr)
break
else
for k2 = 1:8
secline(k2,:) = textscan(fidi, '%f%f%f%f%f',1);
end
secdata{k1} = cell2mat(secline);
k1 = k1+1;
end
end
fclose(fidi);
with:
hdr_1 = sechdr(1,:) % Display Data (Delete)

data_1 = secdata{1}
hdr_LastFull = sechdr(k1-2,:) % Display Data (Delete)
data_LastFull = secdata{k1-2}
producing:
hdr_1 =
1×2 cell array
{[5000]} {[8]}
data_1 =
1.0000e+00 1.6087e-07 2.0500e-01 8.4682e-02 2.8968e-01
2.0000e+00 5.3421e-02 1.0823e-01 1.0074e-02 1.7172e-01
3.0000e+00 3.7223e-02 1.2488e-02 2.2847e-02 7.2557e-02
4.0000e+00 2.0605e-01 2.9866e-02 1.9402e-01 4.2994e-01
5.0000e+00 1.4819e-01 4.3587e-02 5.9918e-02 2.5170e-01
6.0000e+00 6.4602e-02 1.2636e-01 1.2115e-02 2.0308e-01
7.0000e+00 3.7825e-01 2.5960e-01 1.9181e-01 8.2967e-01
8.0000e+00 1.1831e-02 3.4485e-02 3.8945e-02 8.5260e-02
hdr_3219 =
1×2 cell array
{[3223000]} {[8]}
data_3219 =
1.0000e+00 6.1856e-05 2.4395e+01 1.8241e+01 4.2636e+01
2.0000e+00 5.7546e+00 4.7172e+01 1.0499e+01 6.3425e+01
3.0000e+00 9.7735e+00 4.3917e+01 3.9653e+01 9.3343e+01
4.0000e+00 2.5318e+00 3.9988e+01 3.5174e+00 4.6038e+01
5.0000e+00 7.7347e+00 1.4633e+01 6.8984e+00 2.9266e+01
6.0000e+00 5.2029e+00 3.6422e+01 8.2238e+01 1.2386e+02
7.0000e+00 3.1026e+01 1.0219e+01 3.8708e-02 4.1284e+01
8.0000e+00 2.0353e-01 3.2281e+00 8.1987e+00 1.1630e+01
The ‘hdr’ cell array are the first 3 header lines in the file. The ‘sechdr’ cell array are the first row of each section, and ’secdata’ are the matrix following it.
.