Save data from for loop into array

6 views (last 30 days)
Christian Boaitey
Christian Boaitey on 12 Oct 2022
Answered: Voss on 12 Oct 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

Accepted Answer

Voss
Voss on 12 Oct 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

More Answers (0)

Categories

Find more on Animation in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!