How can i print fixed text on UIAxes?

I'm working on an program created with app designer. I used the UIAxes and i wanted to print a couple of information on it, i tried with 'text' but the problem is that the axis of the plot are not always the same so the text will not always be in the same position and most important when i zoom the text move outside the box of the plot.
So is there a way to print something that stays steady (for example top-left) in the plot?

 Accepted Answer

You can set the text's Units to 'normalized' so that the text Position is relative to the axes Position, rather than relative to the axes XLim and YLim.
Example:
ax = axes('XLim',[-2 2],'YLim',[-2 2]);
t = text( ...
'Parent',ax, ...
'String','some text', ...
'Units','normalized', ...
'Position',[0 1]); % top-left
% just for demo, copy stuff to a new figure:
copyobj(gca(),figure())
% now change the axes limits, text stays in top-left:
set(ax,'XLim',[-10 10],'YLim',[50 150])

7 Comments

Thank you, it worked. But now I have a problem to allign it, this is my code:
text(app.UIAxes, 0.5, max(app.UIAxes.YTick) , ...
sprintf (" T=%1.0f%s\n UR=%1.0f%s\n Pd=%1.0f%s\n Sd=%1.0f%s",T,'°C',UR,'%',Pd, ...
' mg/m^2*d',Sd,' mg/m^2*d'), 'Units', 'normalized', 'Position', [0 1]);
And this is what i get:
How do i allign the first line with the limit of Y, so that it all stay in the plot?
have you considered using legend()? You can create fake graphics objects with coordinates nan to have something to hang the text from
do you mean creating a legend and then using its coordinates for the text? if so how can i do that?
No I mean like
h(1) = line(nan, nan, 'linecolor', 'none')
h(2) to h(4) same
legend(h, {'T = 13{\circ}C',other entries}, 'Position', COORDINATES)
@Alessandro Meda: If using text, change the VerticalAlignment to 'top' (the default value is 'middle', which places the middle of the text at the y-location specified by Position(2) - in this case the top of the axes).
You may also want to set the 'EdgeColor' and 'BackgroundColor' and adjust the Position; something like this:
figure();
axes('XLim',[0 20],'YLim',[0 0.9]);
T = 13; UR = 73; Pd = 5; Sd = 0;
text( ...
'String',sprintf (" T=%1.0f%s\n UR=%1.0f%s\n Pd=%1.0f%s\n Sd=%1.0f%s", ...
T,'°C',UR,'%',Pd,' mg/m^2*d',Sd,' mg/m^2*d'), ...
'Units', 'normalized', ...
'Position', [0.01 1], ...
'VerticalAlignment','top', ...
'EdgeColor','k', ...
'BackgroundColor','w');
("Answer" from @Alessandro Meda moved here:)
Thank you, it worked perfectly
Voss
Voss on 3 Jun 2022
Edited: Voss on 6 Jun 2022
You're welcome!

Sign in to comment.

More Answers (0)

Asked:

on 1 Jun 2022

Edited:

on 6 Jun 2022

Community Treasure Hunt

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

Start Hunting!