xxxxxxxxxx
% For matlab to position legend for you, use 'Location'
legend({'A','B'},'Location', 'Best');
% For precise location, use 'Position'
x = 0.2; y=0.6; width=0.1;height=0.2;
legend({'A','B'},'Position',[x y width height]);
https://se.mathworks.com/help/matlab/ref/matlab.graphics.illustration.legend-properties.html
xxxxxxxxxx
x = [0 : 0.01: 10];
y = sin(x);
g = cos(x);
plot(x, y, x, g, '.-'), legend('Sin(x)', 'Cos(x)')