Text location

868 views (last 30 days)
Jorge
Jorge on 6 Feb 2011
Answered: Matyas Mechler on 17 Oct 2021
Is possible put a text in a determinated position, for example, at the top left?

Accepted Answer

Walter Roberson
Walter Roberson on 6 Feb 2011
text(0,1,'Your String','Units','normalized')
  3 Comments
Walter Roberson
Walter Roberson on 5 Oct 2018
h = annotation('textbox', [0 1 0 0], 'String', 'YourString', 'FitBoxToText', true);

Sign in to comment.

More Answers (5)

Jan
Jan on 6 Feb 2011
FigH = figure;
AxesH = axes('Parent', FigH, ...
'Units', 'normalized', ...
'Position', [0, 0, 1, 1], ...
'Visible', 'off', ...
'XLim', [0, 1], ...
'YLim', [0, 1], ...
'NextPlot', 'add');
TextH = text(0,1, 'Top left', ...
'HorizontalAlignment', 'left', ...
'VerticalAlignment', 'top');
Now you can inspect the properties with "get(TextH)" or "set(TextH)" for further adjustments.
Sometimes a UICONTROL('Style', 'text') is easier, because it does not need the invisible full-figure AXES object.

RonE
RonE on 4 Oct 2018
I find the documentation on 'text' to be nearly useless in this matter. The following places text at the top left of the graph.
ylimits = ylim;
ymax = ylimits(2);
vert_spacing = ymax/47; %may have to experiment with this #
text(10, ymax-vert_spacing*1, 'Line 1');
text(10, ymax-vert_spacing*2, 'Line 2');
...

Jiro Doke
Jiro Doke on 6 Feb 2011
What do you mean "top left"? Top left of the figure? Top left of the screen? Top left of the axes?
Have you looked at the function text?
  3 Comments
Jiro Doke
Jiro Doke on 6 Feb 2011
"text" doesn't have that option.

Sign in to comment.


Vieniava
Vieniava on 6 Feb 2011
Edited: John Kelly on 26 Feb 2015

Matyas Mechler
Matyas Mechler on 17 Oct 2021
My solution is somewhat similar to that of RonE.
You can get the limits of the x and y axes:
xL=xlim;
yL=ylim;
(You have to assign them to variables, you cannot simply use e.g. xlim(1).)
Next, you can use these values to specify the location of your text; e.g.:
xT=xL(1)-(xL(2)-xL(1))/6.5;
yT=yL(2)+(yL(2)-yL(1))/17;
Then the text command looks like this:
text(xT,yT,'YourText')

Community Treasure Hunt

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

Start Hunting!

Translated by