How to specify correct fontsize of text according to plot boundary coordinates
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alberto Mora
am 9 Feb. 2021
Kommentiert: Alberto Mora
am 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 Kommentare
Akzeptierte Antwort
Bjorn Gustavsson
am 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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!