41 views (last 30 days)
Show older comments
A Poyser on 25 Jun 2024 at 10:31
Commented: Mathieu NOE on 26 Jun 2024 at 14:31
Accepted Answer: dpb
- Sample1.csv
- Sample2.csv
- Sample3.csv
- Sample4.csv
- Sample5.csv
- Sample6.csv
- Sample7.csv
Open in MATLAB Online
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 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Mathieu NOE on 25 Jun 2024 at 13:57
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2131771-i-have-a-function-that-adds-a-legend-i-want-to-make-the-legend-group-certain-plots-together-yet-so-t#comment_3195356
hello
you could probably use this Fex submission to get to this result
GridLegend - File Exchange - MATLAB Central (mathworks.com)
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")
Sign in to comment.
Sign in to answer this question.
Accepted Answer
dpb on 25 Jun 2024 at 15:14
Edited: dpb on 25 Jun 2024 at 17:46
Open in MATLAB Online
- Sample1.csv
- Sample2.csv
- Sample3.csv
- Sample4.csv
- Sample5.csv
- Sample6.csv
- Sample7.csv
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.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
More Answers (1)
Mathieu NOE on 25 Jun 2024 at 15:23
Open in MATLAB Online
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 Comments Show NoneHide None
Show NoneHide None
A Poyser on 26 Jun 2024 at 14:21
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2131771-i-have-a-function-that-adds-a-legend-i-want-to-make-the-legend-group-certain-plots-together-yet-so-t#comment_3196396
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 on 26 Jun 2024 at 14:31
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/2131771-i-have-a-function-that-adds-a-legend-i-want-to-make-the-legend-group-certain-plots-together-yet-so-t#comment_3196406
ok - no problem .
fyi, you can also vote for other answers that you find useful - that rewards people who spent time on your problem
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office