Setting position of Annotation on an axes component

98 ビュー (過去 30 日間)
Jason
Jason 2020 年 4 月 2 日
コメント済み: DGM 2024 年 10 月 17 日
Hello, I am trying to add an annotation (textbox) to a plot that is on an axes component (Im using GUIDE). I understand you cannot set as a container the axes component so thought I could just get the x,y, coordinates of the axes.
axes(handles.axes1);
p=get(handles.axes1,'Position')
dim = [p(1) p(2) .3 .3];
str = ['Max dist (+/-0.1mm) = ',num2str(mxdist11,'%.4f')];
ann=annotation('textbox',dim,'String',str,'FitBoxToText','on', 'BackgroundColor','#FEBEAD');
But its not showing in the correct location
Surely this should be in one of the corners?
My aim is to get it in the top right corner.
thanks for any help

採用された回答

Jakob B. Nielsen
Jakob B. Nielsen 2020 年 4 月 2 日
編集済み: Jakob B. Nielsen 2020 年 4 月 2 日
Hi Jason,
When you get the position of an axes, the first and second index are the start location of your axes in the x and y dimensions, respectively. The third and fourth index is the width and height of the axes. Try typing this into just the command window, in turn:
ax=axes;
p=get(ax,'Position')
It will probably give you something like
p =
0.1300 0.1100 0.7750 0.8150
(this is for my screen). It means that the x axis starts 0,13 of the way in, from the very leftmost part of the figure, and it then proceeds a further 0,775 whereupon it stops.
So what you want is for the x location of your textbox to be your axes start point, p(1), plus your axes width, p(3), minus the width of your text box. How to get that width? Well you have your ann handle, so ann.Position gives you the width (and height) - again in the 3rd and 4th index.
On my screen resolution this gives a textbox in the top right.
(mind you I hardset axes limits and the string, since I obviously dont have the variables you do :)
ax=axes;
p=get(ax,'Position');
%
w=0.3518;
h=0.0643;
dim = [(p(1)+p(3)-w) (p(2)+p(4)-h) w h];
str = ['Max dist (+/-0.1mm) = 0.7203'];
xlim([-0.8 0.8]);
ylim([-3 5]);
ann=annotation('textbox',dim,'String',str);
  5 件のコメント
Will Reeves
Will Reeves 2024 年 10 月 17 日
It does seem a shame. it would be nice to be able to define "the parent" such that the coordinate system is based off of that. If the parent is an "axis" then use that as the coordinate system. If it's a figure window, use the figure window if it's a tiled layout element, use that etc. ?
DGM
DGM 2024 年 10 月 17 日
It'd be nice, but at the least, you could wrap most of the task up in a simple function to reduce the inconvenience.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by