Is there a way to use a loop to generate outputs for a section of data (like rows 1 through 206) and then moving on to doing the same for subsequent rows within the same loop?

11 ビュー (過去 30 日間)
I am trying to puzzle out some work for my thesis and I've hit a bit of a sumbling block. I am calling to a data set using readmatrix and am analyzing the stress and strain data to generate stress-strain curves and a value for yield stress. My code for this works perfectly, but I have over 100 specimens to do this for and so I am trying to do it in a loop so that I can just type everything in once and hit run.
The dataset that I am calling to has numbers in column 1 that are determining which specimen each row relates to. So I have the value 1 in the rows of the first specimen (so 1 all the way down from the first row to row 206), 2 in the rows for the second, and so on. Is there a way for me to generate either a for loop or while loop so that when "name=1" it goes through the calculations for the first specimen, and then moves onto the "name=2" case and so on down the line without the calculations including data from previous or subsequent specimens? I have a professor that recommended using switch and case when I discussed this with him verbally but I have been unable to go over my code with him.
The code below is for a single specimen just to give you an idea of what I'm doing. I am really at a loss of how I would loop this and I feel like I might be guaranteed to have to write this out 100 times. My thesis advisor would prefer I get this into a loop though so any help you can provide would be greatly appreciated!
%% Retrieving relevant data-
Data=readmatrix('Ti64_Tensile_Data_.csv'); % Importing the data
%Data=readtable('Ti64_Tensile_Data.csv'); % Importing the data
%% AM2 Strain Rate of 0.001 mm/mm/s
Stress=Data(1:206, 8); % Engineered Stress of sample AM2 in MPa
Strain=Data(1:206, 6); % Engineered Strain of sample AM2
% Smoothing the curve
StressS=smooth(Stress);
StrainS=smooth(Strain);
% Using the linear region to calculate Elastic Modulus
f1=fit(StrainS(1:99), StressS(1:99), 'poly1');
p1=coeffvalues(f1);
E=p1(1);
%Strain_Y=Strain+0.002; % Yield Strain
Yield=E*StrainS; % Yield Stress Values
% Finding where to end offset
x=find(Yield<=Stress, 1, 'last')+12;
% Plotting
figure('Name', 'Stress Strain Curve')
plot(StrainS, StressS)
title('Stress-Strain for AM2')
xlabel('\epsilon [mm/mm]')
ylabel('\sigma [MPa]')
grid on
hold on
plot(StrainS(1:x)+0.002, Yield(1:x))
%plot(Strain, Stress)
%plot(f1)
hold off
Yield_Stress=Stress(x);
fprintf('The yield strength for AM2 is: %f \n', Yield_Stress)
% How can I loop with name=data(:, 1) being what determines the iterations?

回答 (3 件)

Walter Roberson
Walter Roberson 2022 年 11 月 4 日
If you are certain that the identifiers in column 1 are always positive integers starting from 1, and that there are no gaps in the numbering, then you can use column 1 as the group identifier directly without needing to findgroups() . Just-in-case you might later want to use non-consecutive identifiers, it would not hurt to use findgroups() anyhow -- the small delay will probably be worth the problems it might save later.
  2 件のコメント
SEAN HACKETT
SEAN HACKETT 2022 年 11 月 4 日
Column 1 is always positive and has no gaps between the numbers, it is the only part of the dataset that I inputted by hand just so that I would be able to identify the the specimens via a number. Would these be applied within a loop, and if so how might that look? Sorry for being dumb, it's been a long year this week.
Walter Roberson
Walter Roberson 2022 年 11 月 4 日
G = Data(:,1);
columns_to_select = [6 8];
results = splitapply(@process_one_id, Data(:,columns_to_select), G);
function output = process_one_id(Strain, Stress)
appropriate stuff goes here
output = Yield_Stress... %or as appropriate
end
If you need to return anything other than a row vector, then return a cell array containing the information you want.
If you need the ID for the processing then you can
G = Data(:,1);
columns_to_select = [1 6 8];
results = splitapply(@process_one_id, Data(:,columns_to_select), G);
function output = process_one_id(IDs, Strain, Stress)
ID = IDs(1); %will all be the same or the group
appropriate stuff goes here
output = Yield_Stress... %or as appropriate
end

サインインしてコメントする。


the cyclist
the cyclist 2022 年 11 月 4 日
編集済み: the cyclist 2022 年 11 月 4 日
Here is one way:
for ii = 1:100
indexToThisSection = (206*(ii-1)+1) : (206*ii);
Stress=Data(indexToThisSection, 8); % Engineered Stress of sample AM2 in MPa
Strain=Data(indexToThisSection, 6); % Engineered Strain of sample AM2
... the rest
end
  1 件のコメント
the cyclist
the cyclist 2022 年 11 月 4 日
This method assumes 206 rows for each section. (It seems from later comments that this is not a valid assumption.)

サインインしてコメントする。


the cyclist
the cyclist 2022 年 11 月 4 日
A somewhat more "advanced" way -- but perhaps more intuitive -- would be to convert Data into a 3-dimensional array first, where each slice is one of the sections, and then process each slice.
For example, if you know that Data has 8 columns, then
Data = reshape(Data,206,8,100); % Reshapes into 100 "slices" of 206x3 matrices
for kk = 1:100
Stress=Data(:, 8, kk); % Engineered Stress of sample AM2 in MPa
Strain=Data(:, 6, kk); % Engineered Strain of sample AM2
... the rest
end
If you don't know the number of columns ahead, you can use the size function to get that info.
  2 件のコメント
SEAN HACKETT
SEAN HACKETT 2022 年 11 月 4 日
There are actually 23 colums in the dataset as a whole, 6 and 8 just happen to be the locations of the tracked strain and stress respectively. Would that affect the 3-D array? Also just to clarify, since 206 is just the ending row for the first specimen and then specimen two runs from 207 to 237 (I apologize, I should have specified this in the question) and then the subsequent specimens all vary in likewise fashion depending on the number of datapoints we were able to capture, would that bug up this method?
the cyclist
the cyclist 2022 年 11 月 4 日
This method assumes 206 rows for each section. (It seems from later comments that this is not a valid assumption.)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStress and Strain についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by