How can I save multiple matrix and index them?

11 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

采纳的回答

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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by