MATLAB: Global legend for all subplots in a figure placed ON the figure (not outside the figure)

legendMATLAB

Hello ๐Ÿ™‚
So, I'd like to make one legend on the second subplot (the big one to the right in the figure) and it has to be one legend for all three graphs with the red one appearing first.
BUT when I try the following as shown by the code below, I can only get the legend placed above the first subplot but below the second subplot, as if the legend belongs to the first subplot, so giving the legend a new position does not solve the problem.
Any help to solve this will be appreciated, thanks ๐Ÿ™‚
Best regards, Henriettte
figure
ax1 = subplot(1,2,1);
hold on
p1 = plot(time1,temp1,'.-','MarkerSize',10,'Color',Cdefault(2,:));
P1 = get(gca, 'Position');
set(gca,'Units','Normalized','Position',P1.*[1 0.7 0.45 0.7])
xlabel('Time [min]','Interpreter','latex')
ylabel('Surface temperature [$^{\circ}$C]','Interpreter','latex')
ax2 = subplot(1,2,2);
p2 = plot(time2,temp2,'.-','MarkerSize',10,'Color',[0.2 0.6 1.0]);
hold on
p3 = plot(time3,temp3,'.-','MarkerSize',10,'Color',[0.0 0.0 0.8]);
P2 = get(gca, 'Position');
set(gca,'Units','Normalized','Position',P2.*[0.55 0.7 2 0.7])
xlim([0 45])
xlabel('Time [min]','Interpreter','latex') % % %
linkaxes([ax1,ax2],'y');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.05, 0.7, 1.0])
yticklabels(ax2,{})
t.TileSpacing = 'none';
legend([p1,p2,p3],{'Brake test on: Heating',…
'Brake test off: Natural cool down',…
'Brake test off + Cooling system on: Forced cool down',…
'Location','NorthEast'})
hold off
(specifications to 'prettyfy' the figure has been left out in the code)

Best Answer

In the mean time I have come around the problem in another way, that is by making a horisontal legend on top instead. Itยดs not what I wanted but looks okay, so this is fine.
/Henriette