Hold off in uitab does not seem to work?

조회 수: 4 (최근 30일)
Old Newbie
Old Newbie 2025년 7월 22일
댓글: Old Newbie 2025년 7월 23일
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
Walter Roberson
Walter Roberson 2025년 7월 23일
delete(gca(ft))
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