How to import multiple text files from multiple folders and take maximum from each text file

조회 수: 3 (최근 30일)
These are the main folders
And each folders contains these txt files, I need to import AllMaxDrift from each pga
  댓글 수: 3
Stephen23
Stephen23 2022년 5월 30일
@Nazanin Farsi: please upload a sample AllMaxDrift data file by clicking the paperclip button.

댓글을 달려면 로그인하십시오.

답변 (5개)

Davide Masiello
Davide Masiello 2022년 5월 29일
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

댓글을 달려면 로그인하십시오.


Nazanin Farsi
Nazanin Farsi 2022년 5월 30일
  댓글 수: 5

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2022년 5월 30일
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
Walter Roberson
Walter Roberson 2022년 5월 30일
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
Davide Masiello 2022년 5월 30일
편집: Davide Masiello 2022년 5월 31일
Hey @Nazanin Farsi, based on your replies, maybe the following code will work.
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
Davide Masiello
Davide Masiello 2022년 6월 3일
@Nazanin Farsi my pleasure. If it worked, it is worth accepting the answer for future reference.

댓글을 달려면 로그인하십시오.


Nazanin Farsi
Nazanin Farsi 2022년 5월 31일

Sure. The Matrix which shows maximums, shows them to row 10, instead of 22 records. It shows zero

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by