I have a function that adds a legend I want to make the legend group certain plots together yet so that instead of a legend with 14 entries there is a legend with 5 entries

1 回表示 (過去 30 日間)
I have managed to make a code that automates the plotting of data but I would like the code to group the plots into disticnt groups.
Let us say samples 1,3,5 - is group 1
and 2,4,6- is group 2
7,9,11 group 3
8,10,12 is group 4
and finally 13 & 14 is group 5
this is the code
Nsamples = 7;
%
L = 100; % coupon gauge section (mm)
A = 17; % coupon cross-sectional area (mm^2)
%
figure(1);
hold;
figure(2);
hold
%
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);
%
figure(1)
h(k) = plot(test(k).disp,test(k).force);
set(h(k),'LineWidth',2);
%
figure(2)
g(k) = plot(test(k).eyy*100,1000*test(k).force/A);
set(g(k),'LineWidth',2);
%
leg_string{k} = ['Sample ' num2str(k)];
%
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
legend(leg_string,'FontSize',28,'Interpreter','latex');
%
%
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
legend(leg_string,'FontSize',28,'Interpreter','latex');
%
%
function [disp,force,eyy] = post_fun(T)
%
disp = table2array(T(:,4));
force = table2array(T(:,end));
eyy = table2array(T(:,10));
%
index = isnan(disp) | isnan(force) | isnan(eyy) | disp < 0 | force < 0 | eyy < 0;
%
disp(index) = [];
force(index) = [];
eyy(index) = [];
%
[~,index] = max(force);
disp(index+1:end) = [];
force(index+1:end) = [];
eyy(index+1:end) = [];
%
end
I have added sample 1-7 . csv but the code doesn't seem to run on this page.
this is what the figure1+2 looks like
Hopefully you can help.
Thanks
Alex
  1 件のコメント
Mathieu NOE
Mathieu NOE 2024 年 6 月 25 日
hello
you could probably use this Fex submission to get to this result
so each group of 3 data could have same color and just different linestyle / markerstyle
other interesting legend tweaks are available on the same Fex (search "legend")

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

採用された回答

dpb
dpb 2024 年 6 月 25 日
編集済み: dpb 2024 年 6 月 25 日
Nsamples = 7;
G=ones(1,2*Nsamples);
G(1:2:5)=1;
G(2:2:6)=2;
G(7:2:11)=3;
G(8:2:12)=4;
G(13:14)=5;
M={'x','+','*','s','d'}; % a morker for each group
C={'r','b','k','m','g'}; % and a color
L = 100; % coupon gauge section (mm)
A = 17; % 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});
figure(2)
g(k) = plot(test(k).eyy*100,1000*test(k).force/A,'LineWidth',2,'Marker',M(ig),'Color',C{ig});
leg_string{k} = ['Group ' 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
legend(h([1 2 7]),leg_string([1 2 7]),'FontSize',28,'Interpreter','latex','location','northwest');
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
legend(g([1 2 7]),leg_string([1 2 7]),'FontSize',28,'Interpreter','latex','location','northwest');
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
The legends to display are those for the first element in each group so the linestyle, marker and color will match the legend string.

その他の回答 (1 件)

Mathieu NOE
Mathieu NOE 2024 年 6 月 25 日
this is a simple solution, where the files are processed in the order that you wished (cf list = [1 3 5 2 4 6 7 9 11 8 10 12 13 14]) , then visually afterwards we force the line color an style to follow a given order so the group is easily identified (appears also in the legend if that helps too)
here the result for figure 1 :
% Nsamples = 7;
%
L = 100; % coupon gauge section (mm)
A = 17; % coupon cross-sectional area (mm^2)
%
%
% pos array
posA = [200 200 1024 768];
f1 = figure('Position',posA,'Color',[1 1 1]); hold on
a1 = gca;
f2 = figure('Position',posA,'Color',[1 1 1]); hold on
a2 = gca;
% line style / colors
colors = [
0 0 1
1 0 0
0 0.75 0
0.5 0 0.5
1 0.5 0
];
styles = {'-','--','-.'};
inds = 0; % index for style indexing
spacing = size(styles,2); % period for style indexing
indc = 1; % index for color indexing
% Let us say samples 1,3,5 - is group 1
% and 2,4,6- is group 2
% 7,9,11 group 3
% 8,10,12 is group 4
% and finally 13 & 14 is group 5
% create a list of groups of 3 data files according to description above :
% list = [1 3 5 2 4 6 7 9 11 8 10 12 13 14]
list = [1 3 5 2 4 6 7 ] % here we have only 7 files
for ck = 1:numel(list)
k = list(ck);
% colors and linestyle index management
inds = inds +1;
if inds >spacing
inds = inds - spacing;
indc = indc +1;
end
%
file_name = ['Sample' num2str(k) '.csv'];
%
T = readtable(file_name,'VariableNamingRule','preserve');
%
[disp,force,eyy] = post_fun(T);
%
h = plot(a1,disp,force,'LineWidth',2,"color",colors(indc,:),"linestyle",styles{inds});
set(a1,'FontSize',24);
xlabel(a1,'Displacement (mm)','FontSize',32,'Interpreter','latex');
ylabel(a1,'Force (kN)','FontSize',32,'Interpreter','latex');
box on; grid on
%
g = plot(a2,eyy*100,1000*force/A,'LineWidth',2,"color",colors(indc,:),"linestyle",styles{inds});
set(a2,'FontSize',24);
xlabel(a2,'Strain (\%)','FontSize',32,'Interpreter','latex');
ylabel(a2,'Stress (MPa)','FontSize',32,'Interpreter','latex');
box on; grid on
%
leg_string{ck,1} = ['Group ' num2str(indc) ' / Sample ' num2str(k)];
%
end
%
legend(a1,leg_string,'FontSize',13,'Interpreter','latex');
legend(a2,leg_string,'FontSize',13,'Interpreter','latex');
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ùù
function [disp,force,eyy] = post_fun(T)
%
% disp = table2array(T(:,4));
% force = table2array(T(:,end));
% eyy = table2array(T(:,10));
d = table2array(T); % one single call to table2array is enough
disp = d(:,4);
force = d(:,end);
eyy = d(:,10);
%
index = isnan(disp) | isnan(force) | isnan(eyy) | disp < 0 | force < 0 | eyy < 0;
%
disp(index) = [];
force(index) = [];
eyy(index) = [];
%
[~,index] = max(force);
disp(index+1:end) = [];
force(index+1:end) = [];
eyy(index+1:end) = [];
%
end
  2 件のコメント
A Poyser
A Poyser 2024 年 6 月 26 日
Thanks, I accepted the other answer just because it was the first that I read. But I have tried this one and it seems to work well too
Mathieu NOE
Mathieu NOE 2024 年 6 月 26 日
ok - no problem .
fyi, you can also vote for other answers that you find useful - that rewards people who spent time on your problem

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

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by