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 ?

 Akzeptierte Antwort

Star Strider
Star Strider am 12 Jan. 2021

0 Stimmen

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.
.

6 Kommentare

Tejas Appaji
Tejas Appaji am 12 Jan. 2021
Thank you for the help. This works, I now have all the sets in 'secdata'. Is there any way I can make a 3d array from this where the matrix is stacked one behind the other ? It just makes it easier for me to plot them and do other calculations.
I am not sure on how to put all the values in the last column and 1st row of each cell in 'secdata' into a variable so that I can do MSD calculations on them.
If it helps I am only intrested in the last column. I can even remove the first 3 header lines, they are not needed. I do not need any other headers stored, but they help. I need a variable, for example A_1 = contains value of the last column from 1st row in all the matrices in 'secdata'. I can modify the same code to get A_2 for the 2nd row and so on.
secdata{1,1} gives me the first 8x5 matrix. I am just not sure how to address the matrix inside the cell.
As always, my pleasure!
If you do not need the first three header lines in ‘hdr’, just ignore them. That loop must be kept in my code however in order for the rest of the code to work. If you do not need the ‘sechdr’ values, just ignore them as well. That line however must also be kept in my code for my code to work.
The cat function apparently does not want to concatenate these, so a loop is the only option:
for k = 1:k1-2
secdata3(:,:,k) = cell2mat(secdata(k));
end
That produces a (8x5x3219) array.
To extract only column 5 from ‘secdata3’:
Column5 = squeeze(secdata3(:,5,:));
produces a (8x3219) double matrix, consisting of only column 5 from each ‘secdata’ matrix. (The squeeze function eliminates the singleton dimension.)
If you only want the first row of the last column, that would be:
Row1_Column5 = Column5(1,:);
producing a (1x3219) vector.
.
Tejas Appaji
Tejas Appaji am 12 Jan. 2021
This works perfectly... Thank you so much..
Star Strider
Star Strider am 12 Jan. 2021
As always, my pleasure!
This was definitely an interesting problem!
Tejas Appaji
Tejas Appaji am 12 Jan. 2021
It definately was interesting. I tried many different things, but no luck. I did not even know some of these functions until I saw your answer. Those are really helpful for further use.
Star Strider
Star Strider am 12 Jan. 2021
Thank you!
I also continue to learn from Answers others post here, since MATLAB is such a large universe.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by