Save data from for loop into array

6 visualizzazioni (ultimi 30 giorni)
Christian Boaitey
Christian Boaitey il 12 Ott 2022
Risposto: Voss il 12 Ott 2022
Hi,
I'm writing a piece of code to analyse some mechanical testing data. I am trying to save the data from the for loop into an array. However, I'm encountering an error each time. It reads an excel table, and then plots the force and extension through iterating through the spreadsheet. I am just can't figure out how to extract the maximum forces into an array. Here is the code.
clc
clear
close all
%% 5mm joints
figure()
A=readtable('Master-coupon-results-cleaned.xlsx','Sheet','5mm');
for i=0:3:(width(A)-1)
Force=A{:,2+i};
Stroke=A{:,3+i};
MaxForces=max(A{:,2+i})
Max(i)=MaxForces
plot(Stroke,Force)
hold on
grid on
xlabel('Displacement (mm)')
ylabel('Force (N)')
title('Force vs displacement graph for 5mm lap joint')
end

Risposta accettata

Voss
Voss il 12 Ott 2022
Max(i)=MaxForces gives an error when i == 0, because indexing starts at 1.
Furthermore, i goes 0, 3, 6, ..., and I imagine you want those values to go into elements 1, 2, 3, ... of Max, so some adjustment is necessary. See below:
clc
clear
close all
%% 5mm joints
figure()
A=readtable('Master-coupon-results-cleaned.xlsx','Sheet','5mm');
for i=0:3:(width(A)-1)
Force=A{:,2+i};
Stroke=A{:,3+i};
MaxForces=max(A{:,2+i});
% Max(i)=MaxForces
Max(i/3+1)=MaxForces; % i==0 -> index 1, i==3 -> index 2, etc.
plot(Stroke,Force)
hold on
grid on
xlabel('Displacement (mm)')
ylabel('Force (N)')
title('Force vs displacement graph for 5mm lap joint')
end
disp(Max);
1.0e+03 * 2.4656 3.0149 2.0699 2.1248 1.6334 2.0154 0.7336 2.4228 2.3124 2.0752

Più risposte (0)

Categorie

Scopri di più su Animation in Help Center e File Exchange

Tag

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by