How can I save multiple matrix and index them?

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.

 Accepted Answer

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 Comments

Thank you so much KSSV !!!
It worked but I need to take the first and the second column of each file because in the end I need to check them as you can see in my code.
I tried to do this (Line 5 and 6):
for i = 1:N-1
data = importdata(txtFiles(i).name) ; % read the data
M(i) = max(data(:,2)) ; % get the max of second column of data
m(i) = M(i)*0.37;
E(i) = data(:,2); %THIS TWO LINE IS NOT WORKING
Y(i) = data(:,1); %THIS TWO LINE IS NOT WORKING
endhj(i) = round(length(E(i))/2);
for h = 1:endhj
if E(h) >= m
x(h) = Y(h);
y(h) = E(h);
end
end
for j = endhj:length(E)
if E(j) >= m
x2(j) = Y(j);
y2(j) = E(j);
end
end
for d = 1:length(x)
if x(d) ~= 0
x_data(d) = x(d);
else
x_data(d) = 1000;
end
end
a(i) = min(x_data);
b(i) = max(x2);
radius(i) = b(i)-a(i);
end
And a got this:
>> Untitled
Unable to perform assignment because the indices on the left side are not compatible with
the size of the right side.
Error in Untitled (line 12)
E(i) = data(:,2);
Is some way to do what I want to do or i have to copy and past my code for each file? You can see in the end I calculate the radius e I want to plot all them in the same graph.
Thanks again !!!
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.
Thanks, it is working ! :)

Sign in to comment.

More Answers (0)

Products

Release

R2019a

Asked:

Tay
on 6 Aug 2020

Commented:

on 24 Nov 2021

Community Treasure Hunt

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

Start Hunting!