How can I generate a legend of two combined figures? I

8 views (last 30 days)
Hello everybody,
I wrote a Skript to combinen two separates figures of gyroscpic data of legs of horses. VR is right front leg HL is left hind leg.
I changed the colour of one into orange for being able to differentiate between them.
Now I am trying to add the legend that both lines are mentioned, but nothing I tried works.
I hope anybody can give me a resolution that works, thank you!
Files of figures are attached, skript is as followed:
close all, clear all;
%um die diagonalen Beinpaare in eine Abbildung zu bekommen, inkl
%Auftrittspunkte
VR= openfig('bueVR150GZ.fig', 'reuse');
HL= openfig('bueHL150GZ.fig', 'reuse');
b = get(gca,'child');
c = get(b,'Type');
b = findobj(gca,'Type','Line');
set(b,'Color',"#D95319");%changes colour
figure %two figures combined in one to visualise synchronicity of leg movement
h1=subplot(1,1,1);
h2=subplot(1,1,1);
copyobj(allchild(get(VR,'CurrentAxes')),h1 );
hold on
copyobj(allchild(get(HL,'CurrentAxes' )),h2);
xlim([0 inf]);
xlim tight;
%xticks(0:10:end)
grid on;
grid minor;
xlabel('Zeit [s]')
ylabel('Winkeländerung [dps]')
title('Auftrittspunkte des diagonalen Beinpaares','FontSize',12);
%legend({'Vorderbein (rechts)'}{'Hinterbein (links)'});
%hleg1 = legend(VR,"Messdaten");
%hleg2 = legend(HL);
%legend(hleg1, hleg2);
ax= gca;
ax.XTick = 0:1:150 ;%damit wir die beschriftung der xAchse eingestellt
%egend(sprintf(%s, ))%steht hier für character und sprintf steht sozusagen
%für einsetzen. dieses wird immer mit einem Befehlt/anweisungangegeben und
%dann, was in dieese anweissung für variablen/elemente eingesetzt werden
%sollten
%legend(get(gca, 'children'), get(get(gca, 'children')));
%set('XTick',min:1:max);
%XLim = get(gca, 'XLim');
%stepBy = 2.5;
%set(gca, 'XTick', XLim(1):stepBy:XLim(2))
%set(gca, "GridLineStyle", "--")
%savefig('1BueVRHLGyroz150AP.fig')

Accepted Answer

dpb
dpb on 2 Sep 2023
Edited: dpb on 2 Sep 2023
%um die diagonalen Beinpaare in eine Abbildung zu bekommen, inkl
%Auftrittspunkte
VR= openfig('bueVR150GZ.fig','invisible');
HL= openfig('bueHL150GZ.fig','invisible');
b = get(gca,'child');
c = get(b,'Type');
b = findobj(gca,'Type','Line');
set(b,'Color',"#D95319");%changes colour
figure %two figures combined in one to visualise synchronicity of leg movement
h1=axes;
copyobj(allchild(get(VR,'CurrentAxes')),h1);
hold on
copyobj(allchild(get(HL,'CurrentAxes')),h1);
xlim([0 inf]);
xlim tight;
%xticks(0:10:end)
grid on;
grid minor;
xlabel('Zeit [s]')
ylabel('Winkeländerung [dps]')
title('Auftrittspunkte des diagonalen Beinpaares','FontSize',12);
legend('Vorderbein (rechts)','Hinterbein (links)','location','southwest');
You were almost there. :) The problem is you created two axes on top of each other by using subplot and put the two in those separate axes instead of creating a (one) new axes into which to put both. And, legend only puts elements that are on the same axes in its list/display so you couldn't put but the one on each. With both in the one axes, then it works as desired.
  3 Comments
dpb
dpb on 4 Sep 2023
Edited: dpb on 4 Sep 2023
Well, the points were plotted as individual points so there are actually a zillion lines on each plot; identifying which are the two solid lines to tell legend which two out of all them to label is the problem...
VR= openfig('bueVR150GZ.fig','invisible');
HL= openfig('bueHL150GZ.fig','invisible');
b = get(gca,'child');
c = get(b,'Type');
b = findobj(gca,'Type','Line');
set(b,'Color',"#D95319");%changes colour
figure %two figures combined in one to visualise synchronicity of leg movement
h1=axes;
copyobj(allchild(get(VR,'CurrentAxes')),h1);
hold on
copyobj(allchild(get(HL,'CurrentAxes')),h1);
xlim([0 inf]);
xlim tight;
%xticks(0:10:end)
grid on;
grid minor;
xlabel('Zeit [s]')
ylabel('Winkeländerung [dps]')
title('Auftrittspunkte des diagonalen Beinpaares','FontSize',12);
hL=findobj(h1,'LineStyle','-')
hL =
2×1 Line array: Line (Messdaten) Line (Messdaten)
legend(hL,'Vorderbein (rechts)','Hinterbein (links)','location','southwest');
If the order is reversed (will depend on the order were initially plotted as to who's first in line), then just reverse the order of the text or the order of the handles.
The above handles show the two lines were labelled so if had known that a priori, could have searched for the specific text, but didn't know anything about that so just looked for the solid lines and, luckily, there are only two of those.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!