How can I save multiple matrix and index them?

22 Ansichten (letzte 30 Tage)
Tay
Tay am 6 Aug. 2020
Kommentiert: Stephen23 am 24 Nov. 2021
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 Kommentare
Tay
Tay am 12 Aug. 2020
Thanks, I used the sprintf to do it :))

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

KSSV
KSSV am 6 Aug. 2020
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 Kommentare
KSSV
KSSV am 7 Aug. 2020
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 am 12 Aug. 2020
Thanks, it is working ! :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by