How to specify correct fontsize of text according to plot boundary coordinates

3 次查看(过去 30 天)
I create a generic plot in matlab. Then I want to create a text into the plot. The text must have a exact size (length in x direction). In other words, I have the x coordinate of the beginning and the end of the text.
The problem is: how can I transform this size constraint (plot encumbrance) into a correct 'fontsize' value of the text?
I attach a image to explain better what I am looking for.
Thank you and best regards.

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2021-2-12
This is the best I can think of:
th = text(-10.05,0.94,'Hello WORLD!'); % Some text-coordinate in a figure of mine
txt_size = get(th,'Extent'); % gives [x0 y0 dx dy] of text-in-axis
txt_end = -9.975; % "Desired" end
while txt_size(1) + txt_size(3) < txt_end
set(th,'fontsize',get(th,'fontsize')+1); % increase fontsize by 1
txt_size = get(th,'Extent'); % get the text extent
drawnow % for dramatic animation!
end
HTH

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by