Hold off in uitab does not seem to work?

5 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2025-7-22
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 个评论
Old Newbie
Old Newbie 2025-7-23
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);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by