How can I save multiple matrix and index them?

조회 수: 24 (최근 30일)
Tay
Tay 2020년 8월 6일
댓글: Stephen23 2021년 11월 24일
Hello,
I don't if it is even possible but I have 9 files with the same name except his number (1, ... 9) = "SiON_D6_L8.5_12Lyrs_NUMBER_CHANGE_HERE_umOfDistance_Ex.txt"
These txt files are 941x2 sized arrays. I need to open this file and find the maximum value within the values in the second column and then continue to do my accounts. The problem is that I am doing it one by one, that is, I have 9 files and I have to copy the program bellow nine times. My goal is to find the radius of the nine file and in the end I will plot the 9 radius together to compare.
Is it some way to do with a loop? Like I can index the file (1,...9) and when I want to access the file I just index it.
clear all;
file1 = load('SiON_D6_L8.5_12Lyrs_1umOfDistance_Ex.txt'); % open file
E1 = file1(:,2); % get second column
Y1 = file1(:,1); % get first column
M1 = max(E1(:)); % get max of the second column
m1 = M1*0.37;
endij1 = round(length(E1)/2);
for i1 = 1:endij1
if E1(i1) >= m1
x1(i1) = Y1(i1);
y1(i1) = E1(i1);
end
end
for j1 = endij1:length(E1)
if E1(j1) >= m1
x21(j1) = Y1(j1);
y21(j1) = E1(j1);
end
end
for d1 = 1:length(x1)
if x1(d1) ~= 0
x_data1(d1) = x1(d1);
else
x_data1(d1) = 1000;
end
end
a1 = min(x_data1);
b1 = max(x21);
radius1 = b1-a1; % radius 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
file2 = load('SiON_D6_L8.5_12Lyrs_2umOfDistance_Ex.txt');
E2 = file2(:,2);
Y2 = file2(:,1);
M2 = max(E2(:));
m2 = M2*0.37;
endij2 = round(length(E2)/2);
% As you can see to open the second file i'm copying the same code again but chaging the variables and file names.
  댓글 수: 3
Tay
Tay 2020년 8월 12일
Thanks, I used the sprintf to do it :))

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

채택된 답변

KSSV
KSSV 2020년 8월 6일
txtFiles = dir("*.txt") ;
N = length(txtFiles) ;
themax = zeros(N,1) ;
% loop for each file
for i = 1:N
data = importdata(txtFiles(i).name) ; % read the data
iwant(i) = max(data(:,2)) ; % get the max of second column of data
end
  댓글 수: 3
KSSV
KSSV 2020년 8월 7일
E(i) = data(:,2);
The above is wrong..you are saving a column,so LHS should be a matrix.
E(:,i) = data(:,2);
The above will work. It is advised to initiatlize the matrix before loop.
Tay
Tay 2020년 8월 12일
Thanks, it is working ! :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by