f1 = figure;
x = linspace(0, 2*pi);
hold on;
plot(x, sin(x));
plot(x, sin(x * .5), '--');
plot(x, cos(x));
plot(x, cos(x * .5),':');
hold off
legend([f1.CurrentAxes.Children(1), f1.CurrentAxes.Children(2)], 'p1', 'p2', Location='NorthEastOutside');
legend(copyobj(f1.CurrentAxes, f1), [f1.CurrentAxes.Children(3), f1.CurrentAxes.Children(4)], 'p3', 'p4', Location='SouthEastOutside');
f1.CurrentAxes.Visible = 'off';
This approach does not create extra variables but uses the Graphics Object Hierarchy of the figure.