Save data from for loop into array
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Voss
2022-10-12
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
