How to index/address the chart width and height using Position option in Text command?

3 views (last 30 days)
Hi all,
I have this line of code. Suppose I want to use text using half of width and height of my figure. How shouldI index/address that in Text command?
Geo = figure()
Geo.Position(1:4) = [ 100 100 560 420]; % buttom left > Width Height <
a = Width./2 %this is where my problem is. how to get that width pixel out of the line above and use it as a position in chart
b = Height./2
text(a,b,(['mean = ']));
xlim([1 5]) %I odn't want to use xlim because everychart of mine will be different.

Accepted Answer

dpb on 6 Jun 2021
Edited: dpb on 6 Jun 2021
pos=Geo.Position; % save as array
x=pos(1)+pos(3)/2; % midpoint position horizontal
y=pos(2)+pos(4)/2; % midpoint position vertical
text(x,y,(['mean = ']));
NB: however, that text() uses the axes positions, not the figure position coordinates that are retuned by the 'Position' property and that are relative to the actual monitor/display, NOT the axes/figure itself. Those two coordinate positions don't relate to each other.
If the idea is to put the text at the middle of the x- and y- current axes, then using
hTxt=text(0.5,0.5,'Mid-Axes Text','units','normalized','horizontalalign','center');
will put the text at the middle axes range irrespective of x-, y- data limits without trying to compute pixel locations.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!