How to specify correct fontsize of text according to plot boundary coordinates
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Alberto Mora
il 9 Feb 2021
Commentato: Alberto Mora
il 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 Commenti
Risposta accettata
Bjorn Gustavsson
il 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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Annotations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!