Hold off in uitab does not seem to work?

4 views (last 30 days)
Old Newbie
Old Newbie on 22 Jul 2025
Commented: Old Newbie on 23 Jul 2025
Hi,
I am trying to programmatically code a simple asset allocation app. I would like the efficient frontier and the associated asset returns to display in another tab, and if the user changes the expected returns, the efficient frontier is updated in the second tab.
The code below works, but the axes are not reset when the user changes the expected return, so the axes are overwritten as shown in the figure. It seems that the hold(ax,'off'); is not working. What am I doing wrong? I have also tried cla(ax,'reset') and ax=newplot(ax);
Example expected returns used as input were [0.23,0.15,0.11] and then [0.14,0.15,0.11].
Thank you!
function []=testApp()
function parButtonPushed(C)
ft=findobj(fig,'Tag','tab2');
er=findobj(fig,'Tag','er');
erData=table2array(er.Data);
tabg=findobj(fig,'Tag','tabg');
p=Portfolio('RiskFreeRate',0.001);
p = setAssetMoments(p,erData,C);
p = setDefaultConstraints(p);
ax = axes('parent',ft);
cla(ax, 'reset'); % also tried
ax=newplot(ax); % also tried
plotFrontier(ax,p,100);
hold(ax,'on');
scatter(sqrt(diag(C)),erData,20,'filled',"MarkerFaceColor",'#808080','Parent',ax);
hold(ax,'off');
tabg.SelectedTab=ft;
end
fig = uifigure;
fig.Position(3)=850;
fig.Position(4)=440;
movegui(fig,'center');
tabgp = uitabgroup(fig);
tabgp.Position(3)=650;
tabgp.Position(4)=340;
tabgp.Tag='tabg';
tab1 = uitab(tabgp,"Title","Expected Returns");
tab2 = uitab(tabgp,"Title", "Output");
tab2.Tag='tab2';
g = uigridlayout(tab1,[2,1]);
g.RowHeight = {99,22};
g.ColumnWidth = {'fit','fit'};
er=zeros(3,1);
C=[0.030766915 0.0252445 0.007653981;
0.0252445 0.049854359 0.011371085;
0.007653981 0.011371085 0.009284787];
t1=array2table(er,"VariableNames","Expected Return","RowNames",{'Asset 1','Asset 2','Asset 3'});
t11=uitable(g,"Data",t1);
t11.Tag='er';
t11.ColumnEditable = repelem(true,1);
btab1=uibutton(g,'Text','Continue',"ButtonPushedFcn", @(src,event) parButtonPushed(C));
btab1.Layout.Row=2;
btab1.Layout.Column = 1;
end

Answers (1)

Walter Roberson
Walter Roberson on 22 Jul 2025
ax = axes('parent',ft);
Each time you execute that statment, you generate a new axes to plot in.
You instead need something like
ax = gca(ft);
gca() will fetch the current axes if one exists, and will otherwise create a new axes within the given parent.
  3 Comments
Old Newbie
Old Newbie on 23 Jul 2025
Thank you, however, that still did not work. It does not delete the axes so the original axes still appear and are still overlapping.
I sorted it out by deleting all the children in the tab before I create the axes i.e.
delete(get(ft,'Children'));
ax = axes('parent',ft);

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!