Creating matrix from a loop between multiple files

Hi! I am currently trying to format a code which runs through the same steps but for multiple different files which have been loaded into the data base. I am unsure as to how to compress it into a some sort of loop if that'd quicken it? This is the first set of code which I want to condense and obtain the outputs RS1, RS2 and RS3 in a matrix.
% Repeat process by calling and averaging the wanted columns in data file 'c1'.
uc1 = c1(:,3);
u1c1 = [mean(uc1) mean(uc1)];
wc1 = c1(:,5);
u3c1 = [mean(wc1) mean(wc1)];
% Calculate standard deviation from the mean for u1 and u3.
upr1_1 = uc1-u1c1;
upr3_1 = wc1-u3c1;
% Compute the product of both deviations
uval1 = upr1_1.*upr3_1;
uva_1 = [mean(uval1) mean(uval1)];
RS1 = uva_1(1,2)/(10e-5);
% Repeat process by calling and averaging the wanted columns in data file 'c2'.
uc2 = c2(:,3);
u1c2 = [mean(uc2) mean(uc2)];
wc2 = c2(:,5);
u3c2 = [mean(wc2) mean(wc2)];
% Calculate standard deviation from the mean for u1 and u3.
upr1_2 = uc2-u1c2;
upr3_2 = wc2-u3c2;
% Compute the product of both deviations
uval2 = upr1_2.*upr3_2;
uva_2 = [mean(uval2) mean(uval2)];
RS2 = uva_2(1,2)/(10e-5);
% Repeat process by calling and averaging the wanted columns in data file 'c3'.
uc3 = c3(:,3);
u1c3 = [mean(uc3) mean(uc3)];
wc3 = c3(:,5);
u3c3 = [mean(wc3) mean(wc3)];
% Calculate standard deviation from the mean for u1 and u3.
upr1_3 = uc3-u1c3;
upr3_3 = wc3-u3c3;
% Compute the product of both deviations
uval3 = upr1_3.*upr3_3;
uva_3 = [mean(uval3) mean(uval3)];
RS3 = uva_3(1,2)/(10e-5);
Then this is another set, which I also want to loop and form a matrix of umc1:5 values.
%C1 @ 0.45m
uc1 = c1(:,3);
umc1 = mean(uc1);
%C2 @ 0.40m
uc2 = c2(:,3);
umc2 = mean(uc2);
%C3 @ 0.35m
uc3 = c3(:,3);
umc3 = mean(uc3);
%C4 @ 0.30m
uc4 = c4(:,3);
umc4 = mean(uc4);
%C5 @ 0.25m
uc5 = c5(:,3);
umc5 = mean(uc5);
Any help would be greatly appreciated, many thanks. Ben

4 Commenti

It is not clear what the relation between the code and the "multiple files loaded into the database" is. Which database? Which files?
As soon as it is clear, how you do this for one file, it is easy to use a loop to do this for many files. See: https://www.mathworks.com/matlabcentral/answers/57446-faq-how-can-i-process-a-sequence-of-files
Ah no problem, earlier on in the file I loaded files c1, c2, c3, c4... into the code, where the files are all large matrices of data.
Jan
Jan il 18 Mar 2022
Modificato: Jan il 18 Mar 2022
This comment does not increase the clarity. You want to modify the code to expand it from using one fileto using a bunch of files. Then the actual computations do not matter, but how you imprt the file. This means, that the posted code is not relecant to the question, but the important code is missing.
Load ('c1.dat')
Load ('c2.dat')
Load ('c3.dat')
% Repeat process by calling and averaging the wanted columns in data file 'c1'.
uc1 = c1(:,3);
u1c1 = [mean(uc1) mean(uc1)];
wc1 = c1(:,5);
u3c1 = [mean(wc1) mean(wc1)];
% Calculate standard deviation from the mean for u1 and u3.
upr1_1 = uc1-u1c1;
upr3_1 = wc1-u3c1;
% Compute the product of both deviations
uval1 = upr1_1.*upr3_1;
uva_1 = [mean(uval1) mean(uval1)];
RS1 = uva_1(1,2)/(10e-5);
% Repeat process by calling and averaging the wanted columns in data file 'c2'.
uc2 = c2(:,3);
u1c2 = [mean(uc2) mean(uc2)];
wc2 = c2(:,5);
u3c2 = [mean(wc2) mean(wc2)];
% Calculate standard deviation from the mean for u1 and u3.
upr1_2 = uc2-u1c2;
upr3_2 = wc2-u3c2;
% Compute the product of both deviations
uval2 = upr1_2.*upr3_2;
uva_2 = [mean(uval2) mean(uval2)];
RS2 = uva_2(1,2)/(10e-5);
% Repeat process by calling and averaging the wanted columns in data file 'c3'.
uc3 = c3(:,3);
u1c3 = [mean(uc3) mean(uc3)];
wc3 = c3(:,5);
u3c3 = [mean(wc3) mean(wc3)];
% Calculate standard deviation from the mean for u1 and u3.
upr1_3 = uc3-u1c3;
upr3_3 = wc3-u3c3;
% Compute the product of both deviations
uval3 = upr1_3.*upr3_3;
uva_3 = [mean(uval3) mean(uval3)];
RS3 = uva_3(1,2)/(10e-5);

Accedi per commentare.

 Risposta accettata

Stephen23
Stephen23 il 18 Mar 2022
Modificato: Stephen23 il 18 Mar 2022
Use arrays and indexing rather than numbered variable names.
Use a more appropriate tool for importing textfile data, e.g. READMATRIX
P = 'absolute or relative path to where the files are saved';
V = 1:3;
RSV = nan(size(V));
for k = 1:numel(V)
F = sprintf('c%u.dat',V(k));
c1 = readmatrix(fullfile(P,F));
uc1 = c1(:,3);
u1c1 = [mean(uc1),mean(uc1)];
wc1 = c1(:,5);
u3c1 = [mean(wc1),mean(wc1)];
% Calculate standard deviation from the mean for u1 and u3.
upr1_1 = uc1-u1c1;
upr3_1 = wc1-u3c1;
% Compute the product of both deviations
uval1 = upr1_1.*upr3_1;
uva_1 = [mean(uval1),mean(uval1)];
RSV(k) = uva_1(1,2)/(10e-5);
end

Più risposte (0)

Categorie

Prodotti

Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by