views:

40

answers:

1

I've got a GUI in MATLAB with a set of axes pre-placed. I'm using the location property of the legend to place it to the right hand side of the axes. However, by doing this the axes get re-scaled so that the axes+legend take up the original width of the axes. Is there any way to circumvent the re-size?

Example:

x=0:.1:10;
y=sin(x);
figure
pos=get(gca,'position');
pos(3)=.5; %#re-size axes to leave room for legend
set(gca,'position',pos)
plot(x,y)

So far I get:

alt text

Place legend:

legend('sin(x)','location','eastoutside')

...aaaaand...

alt text

MATLAB squishes it all into the original axes space. Any way around this?

+4  A: 

EDIT

%# create three axes with custom position
x=0:.1:10;
y=sin(x);
hAx1 = axes('Position',[0.05 0.05 0.7 0.2]); plot(hAx1, x,y)
hAx2 = axes('Position',[0.05 0.4 0.7 0.2]); plot(hAx2, x,y)
hAx3 = axes('Position',[0.05 0.75 0.7 0.2]); plot(hAx3, x,y)

%# add legend to middle one
h = legend(hAx2, 'sin(x)'); pos = get(h,'position');
set(h, 'position',[0.8 0.5 pos(3:4)])

alt text

Amro
Yes, I know it's done automatically, but I have three sets of axes stacked vertically which plot separate data sets from a simultaneous aquisition. The labels are date stamps, so I only need one legend. The problem is when I only add the legend to the center plot, the other two don't line up any more. I've got a blank on the end of the GUI with enough room for the legend, and I want to place it there.
Doresoom
you can always manually set its `Position` property to fit your layout
Amro
@Doresoom: I added an example using the idea above.
Amro
@Amro: Yeah, I just got this working too - your suggestion of editing the position property got me on the right track.
Doresoom

related questions