Search code examples
matlabtabsmatlab-figuretoolbarparent

Axes toolbar is not shown when axes is re-parented


In MATLAB R2019a (Update 2), when using the following code, the axes toolbar is not shown when selecting Tab2 or Tab3.

classdef test < handle
    properties
        Ax
    end
    methods
        function self = test()
            f = figure;
            tg = uitabgroup(f, 'SelectionChangedFcn', @self.onSelectionChanged);
            t1 = uitab(tg, 'Title', 'tab1');
            uitab(tg, 'Title', 'tab2');
            uitab(tg, 'Title', 'tab3');
            self.Ax = axes(t1);
        end

        function onSelectionChanged(self, ~, e)
            self.Ax.Parent = e.NewValue;

        end
    end
end

I would like to know how to make the toolbar appear in the selected tab.

Edit: The following code shows the problem better: The axes toolbar is stucked in the first panel, while the axes is in second panel. However, to reproduce this, don't copy and paste into your MATLAB command window. It only happens if the axes' parent changes after the axes toolbar is drawn. Thus I split the code into two sections:

f = figure;
p1 = uipanel(f, 'Units', 'normalized', 'Position', [0 0 .5 1]);
p2 = uipanel(f, 'Units', 'normalized', 'Position', [.5 0 .5 1]);
ax = axes(p1);

And now change the axes' parent:

ax.Parent = p2;

Here is the result:Result after re-parenting the axes

And here is what I expect:

expected result

So far, I haven't figured out how to trigger the event that puts the toolbar in the correct position.

Neither changing the Visible property of the figure, axes, axtoolbar or uipanel helped. Also, overriding the toolbar by a new one does not help.


Solution

  • Since I considered the behaviour described above a bug, I have filed it to Matlab staff.

    They responded to get around this issue, one shoud re-set the Parent of the axes toolbar. In case of the test class from above:

    function onSelectionChanged(self, ~, e)
        self.Ax.Parent = e.NewValue;
        set(self.Ax.Toolbar,'Parent',[],'Parent',self.Ax);
    end