How to specify correct fontsize of text according to plot boundary coordinates
3 views (last 30 days)
Show older comments
Alberto Mora
on 9 Feb 2021
Commented: Alberto Mora
on 12 Feb 2021
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.
0 Comments
Accepted Answer
Bjorn Gustavsson
on 12 Feb 2021
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
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!