Using a for loop to read in 16 data sheets and storing one value from each data sheet in a vector - full problem explained
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Fabian Hickson
le 8 Juin 2022
Commenté : Mathieu NOE
le 8 Juin 2022
I have currently got a for loop that reads in all the data values from 16 excel spreadsheets (4 columns, 300+ rows).
I then perform a series of calculations with these data points and I want to store just one value from each - creating a (16, 1) vector where the value from the first data sheet is stored in first row, the second value calculated from the second data sheet is stored in row 2 and so on.
The value I am trying to store is mSTRESS = min(y1) for each data sheet - the max stress experienced by each sample.
The code:
datafiles = dir('Synthetic_Pancreas_Sample_*.xlsx');
figure();
axis padded;
grid on;
xlabel('Strain', 'Interpreter', 'latex', 'fontsize', 16, 'Color', 'k');
ylabel('Stress / MPa', 'Interpreter', 'latex', 'fontsize', 16, 'Color', 'k');
set(gca, 'TickLabelInterpreter', 'latex', 'fontsize', 14);
lgd = legend('fontsize', 10, 'Interpreter', 'latex', 'location', 'Southeast');
title(lgd, 'Sample Number:');
legend('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', ...
'11','12','13','14','15','16');
hold on
A_A = zeros(16,1);
for i = 1:16
A_A(i,:) = mSTRESS;
for file = datafiles'
currentfile = file.name;
RAW = readmatrix(currentfile);
t = RAW(:,1);
mm = RAW(:,2);
N = RAW(:,3);
d = RAW(:,4);
D = max(d);
r = (D/2);
Area = pi*(r^2);
h = RAW(:,5);
H = max(h);
c1 = RAW(:,6);
c1 = max(c1);
c2 = RAW(:,7);
c2 = max(c2);
c3 = RAW(:,8);
c3 = max(c3);
Stress = N./Area;
Strain = mm./H;
I = [0, 0];
XY = [I; Stress Strain];
X = any(XY(:,2)<-0.5,2);
XY(X,:) = [];
STRESS = XY(:,1);
STRAIN = XY(:,2);
STRESS(isnan(STRESS(:))) = [];
STRAIN(isnan(STRAIN(:))) = [];
%plot(STRAIN(1:50:end), STRESS(1:50:end),'color',[c1 c2 c3],'LineWidth',1.5);
p = polyfit(STRAIN,STRESS,4);
x1 = -0.5:0.025:0;
y1 = polyval(p,x1);
plot(x1,y1,'color',[c1 c2 c3],'LineWidth',1.5);
%plot(D, H, '*','color',[c1 c2 c3],'LineWidth',1.5);
mSTRESS = min(y1);
end
0 commentaires
Réponse acceptée
Mathieu NOE
le 8 Juin 2022
hello
I don't see why the code needs two for loops
seems to me one suffice and this code gives the answer in mSTRESS (vector of same dimension as number of files, whatever the quantity, 3 or 16 , no need to specify it )
datafiles = dir('Synthetic_Pancreas_Sample_*.xlsx');
figure();
axis padded;
grid on;
xlabel('Strain', 'Interpreter', 'latex', 'fontsize', 16, 'Color', 'k');
ylabel('Stress / MPa', 'Interpreter', 'latex', 'fontsize', 16, 'Color', 'k');
set(gca, 'TickLabelInterpreter', 'latex', 'fontsize', 14);
lgd = legend('fontsize', 10, 'Interpreter', 'latex', 'location', 'Southeast');
title(lgd, 'Sample Number:');
legend('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', ...
'11','12','13','14','15','16');
hold on
for k = 1:length(datafiles)
currentfile = datafiles(k).name;
RAW = readmatrix(currentfile);
t = RAW(:,1);
mm = RAW(:,2);
N = RAW(:,3);
d = RAW(:,4);
D = max(d);
r = (D/2);
Area = pi*(r^2);
h = RAW(:,5);
H = max(h);
c1 = RAW(:,6);
c1 = max(c1);
c2 = RAW(:,7);
c2 = max(c2);
c3 = RAW(:,8);
c3 = max(c3);
Stress = N./Area;
Strain = mm./H;
I = [0, 0];
XY = [I; Stress Strain];
X = any(XY(:,2)<-0.5,2);
XY(X,:) = [];
STRESS = XY(:,1);
STRAIN = XY(:,2);
STRESS(isnan(STRESS(:))) = [];
STRAIN(isnan(STRAIN(:))) = [];
%plot(STRAIN(1:50:end), STRESS(1:50:end),'color',[c1 c2 c3],'LineWidth',1.5);
p = polyfit(STRAIN,STRESS,4);
x1 = -0.5:0.025:0;
y1 = polyval(p,x1);
plot(x1,y1,'color',[c1 c2 c3],'LineWidth',1.5);
%plot(D, H, '*','color',[c1 c2 c3],'LineWidth',1.5);
mSTRESS(k) = min(y1); % <= here
end
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Stress and Strain dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!