MATLAB Answers

Replace a subplot title on a figure from another function in appdesigner

4 views (last 30 days)
Jason
Jason on 17 Jun 2020
Commented: Jason on 18 Jun 2020
Hello, In appdesigner I have created an image on a subplot in a figure:
f1=figure('position',pos);
s1=subplot(1,3,1)
Im using a roi.ellipse object with an event listener, to allow the user to move and resize the ellipse.(all works fine)
h = images.roi.Ellipse(gca,'Center',[xpeak xpeak2],'Semiaxes',[1.699*fwhm/2 1.699*fwhm2/2],'Color','g','StripeColor','r','LineWidth',1); %1/e^2 = 1.699xfwhm
el=addlistener(h,'ROIMoved',@app.allevents);
what I want to do, is in the function "allevents" where I obtain the moved ellipse parameters, is to calculate the area and then replace the title of the sublot s1 with the area. But I'm having issues with changing the title.
function allevents(app,src,evt)
evname = evt.EventName;
switch(evname)
case{'MovingROI'}
disp(['ROI moving Current Center: ' mat2str(evt.CurrentCenter)]);
disp(['ROI moving Current SemiAxes: ' mat2str(evt.CurrentSemiAxes)]);
case{'ROIMoved'}
disp(['ROI moved Current Center: ' mat2str(evt.CurrentCenter)]);
disp(['ROI moved Current SemiAxes: ' mat2str(evt.CurrentSemiAxes)]);
a=evt.CurrentSemiAxes(1)
b=evt.CurrentSemiAxes(2)
area=pi*app.a*app.b
title(s1,['Area=',num2str(area,'%.1f')])
end
end

  1 Comment

Jason
Jason on 17 Jun 2020
I have found this works:
h=get(gcf,'children')
title(h(3),['Area=',num2str(area,'%.1f')])
But Im not sure I shoud rely on the subplot(1,3,1) always being h(3).

Sign in to comment.

Accepted Answer

Prince Sachdeva
Prince Sachdeva on 18 Jun 2020
As per my understanding, you want to add a title to the sub plot after some pre-processing.
It seems to me that the below solution is working in the latest version:
f1=figure;
s1 = subplot(1, 3, 1)
title(s1,'Area')
The get(gcf, ‘children’) function returns the handles for the sub plots in the reverse order (i.e., the last subplot as the first handle), as you may have observed. So, you can reverse it again as shown below:
h=get(gcf,'children')
h=h(end:-1:1)
title(h(1),'G1')
title(h(2),'G2')
title(h(3),'G3')
Now, you can use h(1) for subplot(1, 3, 1).

  1 Comment

Jason
Jason on 18 Jun 2020
Thankyou for confirming that with my subplot consisting of 3 subplots, the firat one drawn i.e. subplot (1,3,1) will ALWAYS be h(3). Thats all I wanted to confirm

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!