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.
댓글 수: 0
채택된 답변
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 Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!