Save data from for loop into array
6 views (last 30 days)
Show older comments
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
2 Comments
Accepted Answer
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);
0 Comments
More Answers (0)
See Also
Categories
Find more on Animation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!