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?
0 件のコメント
回答 (3 件)
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 件のコメント
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
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
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
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
2 件のコメント
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 Exchange で Stress and Strain についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!