Determine size of text objects

20 ビュー (過去 30 日間)
Yaqub
Yaqub 2012 年 12 月 11 日
I'd like to be able to determine the size of a text object, in pixels. I want to be able to do this in order to create long xlabels for a figure, and rotate them, and finally adjust the axes base in order that the text does not disappear along the bottom of the figure.

採用された回答

Walter Roberson
Walter Roberson 2012 年 12 月 11 日
編集済み: Walter Roberson 2012 年 12 月 11 日
Create a uicontrol('Style', 'text') with FontName and FontSize as appropriate. The Position of the control does not matter, and you can create it with visibility turned off. Make sure the Units property is set to pixels.
Now, set the control's String property to the text you want. Then, get() the Extent property of the control. The returned height and width are the height and width that would be required to render just that String by itself. This will be the case even if the Position of the control was too small (or too wide) for the string: Extent will tell you the size you actually need for it.
You can of course re-use the same uicontrol to size different strings, by setting the String property and reading out the Extent afterwards.
Delete the unneeded uicontrol after you have sized everything.
  1 件のコメント
Yaqub
Yaqub 2012 年 12 月 11 日
Extent property was what I was after ... Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by