File: ef230-2023-08/www/examples/text_placement_example.m Download
%! Example of centering text in a figure
% University of Tennessee : EF 230 Fall, 2009 : Will Schleter

clear all; close all; clc;

% option 1 - known axis limits
xlow=0;
xhigh=100;
ylow=70;
yhigh=80;
axis([xlow xhigh ylow yhigh])
x = xlow + .5*(xhigh-xlow); % centered
y = ylow + .25*(yhigh-ylow); % 25% up
text(x,y,'Example 1','HorizontalAlignment','center')

% option 2 - retrieving the current axis limits
xlim = get(gca,'xlim'); % gca is the handle for the current axis
ylim = get(gca,'ylim');
xlow = xlim(1);
xhigh = xlim(2);
ylow = ylim(1);
yhigh = ylim(2);
x = xlow + .5*(xhigh-xlow); % centered
y = ylow + .5*(yhigh-ylow); % centered
text(x,y,'Example 2','HorizontalAlignment','center')

% option 3 - using normalized units
% centered in x, 75% up in y
text(.5,.75,'Example 3','HorizontalAlignment','center','units','normalized')