uistack on legend and axes objects

9 views (last 30 days)
Cameron Brown
Cameron Brown on 14 Jun 2016
Commented: Cameron Brown on 27 Jun 2016
I have several legends which I would like to be stacked on top of the AXES objects in a figure window. I used to be able to use UISTACK to achieve this, but after upgrading to R2015b it no longer works.
Here is an example:
% make some data for plotting
x=randn(100,1);
y=2*x + randn(100,1);
z=x.^2 + randn(100,1);
% generate plots and legends
hFig=figure;
subplot(2,1,1)
plot(x,y,'bx','DisplayName','Linear')
hL1=legend('show');
subplot(2,1,2)
plot(x,z,'rx','DisplayName','Quadratic')
hL2=legend('show');
% move legends over the opposite plot to visualize stacking
hL1.Position=[0.79 0.32 0.2 0.2];
hL2.Position=[0.79 0.57 0.2 0.2];
% try to reorder stack to put legends on top
uistack([hL1 hL2],'top')
% display object stack order (legends have not been moved to top of stack)
disp(hFig.Children)
I want the stack to be: Legend, Legend, Axes, Axes However, after using UISTACK, the stack order is still: Legend, Axes, Legend, Axes
Is there a workaround to get the desired stack order?

Answers (1)

Debarati Banerjee
Debarati Banerjee on 23 Jun 2016
Hi Cameron,
I made a few changes to the programme and I believe it is working now. Can you check this?
% make some data for plotting
x=randn(100,1);
y=2*x + randn(100,1);
z=x.^2 + randn(100,1);
% generate plots and legends
hFig=figure;
subplot(2,1,1)
plot(x,y,'bx','DisplayName','Linear')
hL1=legend('show');
subplot(2,1,2)
plot(x,z,'rx','DisplayName','Quadratic')
hL2=legend('show');
% move legends over the opposite plot to visualize stacking
% hL1.Position=[0.79 0.32 0.2 0.2];
% hL2.Position=[0.79 0.57 0.2 0.2];
set(hL1,'Position',[0.79 0.32 0.2 0.2]) %Struct field assignment overwrites a value with class "double", so not doing hL1.Position
set(hL2,'Position',[0.79 0.57 0.2 0.2])
% try to reorder stack to put legends on top
uistack([hL1 hL2],'top')
% display object stack order (legends have not been moved to top of stack)
disp(get(hFig,'Children'))
Cheers,
Debarati
  1 Comment
Cameron Brown
Cameron Brown on 27 Jun 2016
This does not solve the problem I am referring to in my question above. The code I have provide runs without errors in MATLAB 2014b or later.
I guess you are running an earlier MATLAB version. The error you refer to: "Struct field assignment overwrites a value with class "double"" will not occur in later versions of MATLAB.
I have only had problems with setting the legend stacking in 2015b. I had no problems in 2014a and earlier.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!