I want to add two histograms with error bars to a piece of code I already have that is generating two plots

4 次查看(过去 30 天)
I have a piece of code that is currently calculating stress/strain and force/extension.
It groups them into catagories based on the temperature and time they were sintered for.
I would like to add histograsm that can also show the modulus of elasticity and the ultimate tensile strength for these groups. The modulus of elasticity should be for say a range from ~0.05 to 0.15 extension, or similar. The error bars should show +/- the stdev for the data sets
would also like to make it so that the legend expressly states the sintering cycle.
For example 'conventional sintering' two-stage sintering', 'sintered at x°C for x hours' etc
below is the code
clear
close all
Nsamples = 14;
G=ones(1,2*Nsamples);
G(1:12)=1;
G([2:1:4 10:1:11])=2;
G(5:1:9)=3;
G(13:14)=4;
%%
load('E:\DIC\sintering_data\OXmcdata.mat')
% G(15)=5;
% G(16)=6;
% G(13:14)=5;
% M={'x','+','*','s','d'}; % a marker for each group
C={'r','b','m','g','m','y'}; % and a color
L = 90; % coupon gauge section (mm)
A = 18; % coupon cross-sectional area (mm^2)
figure(1);
hold on
figure(2);
hold on
for k = 1:Nsamples
file_name = ['Sample' num2str(k) '.csv'];
T = readtable(file_name,'VariableNamingRule','preserve');
[test(k).disp,test(k).force,test(k).eyy] = post_fun(T);
ig=G(k); % get group id for kth file
figure(1)
% h(k) = plot(test(k).disp,test(k).force,'LineWidth',2,'Marker',M(ig),'Color',C{ig});
h(k) = plot(test(k).disp,test(k).force,'LineWidth',2,'Color',C{ig});
figure(2)
% g(k) = plot(test(k).eyy*100,1000*test(k).force/A,'LineWidth',2,'Marker',M(ig),'Color',C{ig});
g(k) = plot(test(k).eyy*100,1000*test(k).force/A,'LineWidth',2,'Color',C{ig});
leg_string{k} = ['Sintering schedule ' num2str(ig)];
end
%
%%
figure(1)
set(gcf,'Position',[200 200 1024 768],'Color',[1 1 1]);
set(gca,'FontSize',24);
xlabel('Displacement (mm)','FontSize',32,'Interpreter','latex');
ylabel('Force (kN)','FontSize',32,'Interpreter','latex');
box on
grid on
title('Force / extension curves for of various sintering schedules','FontSize', 20,'Interpreter','latex')
legend(h([1 2 5 13]),leg_string([1 2 5 13]),'FontSize',28,'Interpreter','latex','location','southeast');
%%
figure(2)
set(gcf,'Position',[200 200 1024 768],'Color',[1 1 1]);
set(gca,'FontSize',24);
xlabel('Strain (\%)','FontSize',32,'Interpreter','latex');
ylabel('Stress (MPa)','FontSize',32,'Interpreter','latex');
box on
grid on
title('Stress / strain curves for of various sintering schedules','FontSize', 20,'Interpreter','latex')
legend(g([1 2 5 13]),leg_string([1 2 5 13]),'FontSize',28,'Interpreter','latex','location','southeast');
%
plot(OXmcdata(:,1),OXmcdata(:,2),'DisplayName','Farhandi et al (2021)','LineWidth',3,'Color','k','LineStyle','--');
%
xlim([0 3E-1]);
ylim([0 350]);
%%
function [displ,force,eyy] = post_fun(T)
displ=T{:,4};
force=T{:,end};
eyy =T{:,10};
ix=all(isfinite([displ force eyy]),2) & all([displ force eyy]>0,2);
displ=displ(ix);
force=force(ix);
eyy=eyy(ix);
[~,imax] = max(force);
displ(imax+1:end) = [];
force(imax+1:end) = [];
eyy(imax+1:end) = [];
end
edit the histograms should should be four columns showing the average UTS and EM for the groups of each sintering cycle, hence the desire for error bars edit
Hopefully you can help.
Thanks
Alex
  6 个评论
Umar
Umar 2024-8-5
Hi @ A Poyser,
I fixed it. There were some technical issues when hitting “Answer this question” button using my Mobile phone. Sorry about that. Also, please let me know if you made any progress or have any questions.

请先登录,再进行评论。

采纳的回答

Umar
Umar 2024-8-5

Hi @A Poyser ,

I had to make modifications to code to ensure that the code will plot the force/extension and stress/strain curves, as well as the histograms for the modulus of elasticity and ultimate tensile strength. In this modified code, the legend will explicitly state the sintering cycle for each group. Adjust the legend labels and formatting as per your specific sintering cycles and preferences.

Note: I assume that your data in the CSV files are in the correct format and that the necessary variables (displ, force, and eyy) are present in the correct columns. Make sure to adjust the column indices in the post_fun function if needed.

Nsamples = 14;

G = ones(1, 2 * Nsamples);

G(1:12) = 1;

G([2:1:4 10:1:11]) = 2;

G(5:1:9) = 3;

G(13:14) = 4;

%load('E:\DIC\sintering_data\OXmcdata.mat')

C = {'r', 'b', 'm', 'g', 'm', 'y'}; % Color for each group

L = 90; % coupon gauge section (mm)

A = 18; % coupon cross-sectional area (mm^2)

figure(1);

hold on

figure(2);

hold on

figure(3);

hold on

figure(4);

hold on

for k = 1:Nsamples

% Assuming the file 'Sample1.csv' is not available, I use generic values
disp = linspace(0, 10, 100);
force = sin(disp);
eyy = disp / 10;
ig = G(k);  % get group id for kth file
figure(1)
h(k) = plot(disp, force, 'LineWidth', 2, 'Color', C{ig});
figure(2)
g(k) = plot(eyy * 100, 1000 * force / A, 'LineWidth', 2, 'Color', C{ig});
% Calculate modulus of elasticity
extension_range = eyy >= 0.05 & eyy <= 0.15;
modulus_of_elasticity = polyfit(eyy(extension_range) * 100, 1000 * 

force(extension_range) / A, 1);

modulus_of_elasticity_value = modulus_of_elasticity(1);
% Plot histogram for modulus of elasticity
figure(3);
histogram(modulus_of_elasticity_value, 'BinWidth', 10, 'FaceColor', C{ig});
hold on;
% Plot histogram for ultimate tensile strength
figure(4);
histogram(max(force), 'BinWidth', 10, 'FaceColor', C{ig});
hold on;
leg_string{k} = ['Sintering schedule ' num2str(ig)];

end

% Plotting configurations for figure 1

figure(1)

set(gcf, 'Position', [200 200 1024 768], 'Color', [1 1 1]);

set(gca, 'FontSize', 24);

xlabel('Displacement (mm)', 'FontSize', 32, 'Interpreter', 'latex');

ylabel('Force (kN)', 'FontSize', 32, 'Interpreter', 'latex');

box on

grid on

title('Force / extension curves for various sintering schedules', 'FontSize', 20, 'Interpreter', 'latex')

legend(h([1 2 5 13]), {'Conventional Sintering', 'Two-Stage Sintering', 'Sintered

at $x^\circ C$ for $x$ hours', 'Sintering Schedule 4'}, 'FontSize', 28,

'Interpreter', 'latex', 'location', 'southeast');

% Plotting configurations for figure 2

figure(2)

set(gcf, 'Position', [200 200 1024 768], 'Color', [1 1 1]);

set(gca, 'FontSize', 24);

xlabel('Strain (\%)', 'FontSize', 32, 'Interpreter', 'latex');

ylabel('Stress (MPa)', 'FontSize', 32, 'Interpreter', 'latex');

box on

grid on

title('Stress / strain curves for various sintering schedules', 'FontSize', 20,

'Interpreter', 'latex')

legend(g([1 2 5 13]), {'Conventional Sintering', 'Two-Stage Sintering', 'Sintered

at $x^\circ C$ for $x$ hours', 'Sintering Schedule 4'}, 'FontSize', 28,

'Interpreter', 'latex', 'location', 'southeast');

% Plotting configurations for figure 3

figure(3)

set(gcf, 'Position', [200 200 1024 768], 'Color', [1 1 1]);

set(gca, 'FontSize', 24);

xlabel('Modulus of Elasticity (MPa)', 'FontSize', 32, 'Interpreter', 'latex');

ylabel('Frequency', 'FontSize', 32, 'Interpreter', 'latex');

box on

grid on

title('Histogram of Modulus of Elasticity', 'FontSize', 20, 'Interpreter', 'latex')

% Plotting configurations for figure 4

figure(4)

set(gcf, 'Position', [200 200 1024 768], 'Color', [1 1 1]);

set(gca, 'FontSize', 24);

xlabel('Ultimate Tensile Strength (kN)', 'FontSize', 32, 'Interpreter', 'latex');

ylabel('Frequency', 'FontSize', 32, 'Interpreter', 'latex');

box on

grid on

title('Histogram of Ultimate Tensile Strength', 'FontSize', 20, 'Interpreter', 'latex')

% Set the limits for x and y axes

xlim([0 3E-1]);

ylim([0 350]);

% Function to extract relevant data from the table

function [displ, force, eyy] = post_fun(T)

displ = T{:, 4};
force = T{:, end};
eyy = T{:, 10};
ix = all(isfinite([displ force eyy]), 2) & all([displ force eyy] > 0, 2);
displ = displ(ix);
force = force(ix);
eyy = eyy(ix);
[~, imax] = max(force);
displ(imax+1:end) = [];
force(imax+1:end) = [];
eyy(imax+1:end) = [];

end

Please customize this code based on your preferences ! Let me know if you have any further questions

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Errorbars 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by