How to import multiple text files from multiple folders and take maximum from each text file
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
These are the main folders ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1014725/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1014725/image.png)
And each folders contains these txt files, I need to import AllMaxDrift from each pga
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1014730/image.png)
Antworten (5)
Davide Masiello
am 29 Mai 2022
clear,clc
n = 0.1:0.1:1.3;
for k = 1:length(n)
filename = [num2str(n(k)),'pga/AllMaxDrift.out'];
data = readmatrix(filename);
max_value(k) = max(data);
end
Something like this should work, although it strictly depends on the way the data inside the .out files are structured.
3 Kommentare
Walter Roberson
am 30 Mai 2022
I cannot be sure without a file to test with, but I suspect
n = 0.1:0.1:1.3;
for k = 1:length(n)
filename = [num2str(n(k)),'pga/AllMaxDrift.out'];
data = load(filename, '-ascii');
max_value(k) = max(data);
end
3 Kommentare
Walter Roberson
am 30 Mai 2022
You do not need to open the files in MATLAB: that code will open the files for you.
You can paste that code into the command line if you want. Or you can store it into a .m file and execute the .m file (which is what I would recommend.)
Davide Masiello
am 30 Mai 2022
Bearbeitet: Davide Masiello
am 31 Mai 2022
n1 = 1:22;
n2 = 0.1:0.1:1.3;
max_value = zeros(length(n1),length(n2));
for row = 1:length(n1)
for col = 1:length(n2)
filename = ['R',num2str(n1(row),'%i'),'/data/',num2str(n2(col),'%.1f'),'pga/AllMaxDrift.out'];
data = importdata(filename);
max_value(row,col) = data(3,15);
end
end
This is based on the email you sent me saying you need the element in the 3rd row and 15th column of every file.
Please note that the max values are now stored in a matrix.
22 Kommentare
Davide Masiello
am 3 Jun. 2022
@Nazanin Farsi my pleasure. If it worked, it is worth accepting the answer for future reference.
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!