Adding text programmatically to a figure

I have a figure with several subplots in it. I want to place a textbox with some text init which belongs to the entire figure. However, whenever I try to use the text command to place the text, it places it relative to the current subplot but not to the overall figure. How do I overcome this nuance?
Thanks in advance.

 採用された回答

DGM
DGM 2022 年 1 月 3 日
編集済み: DGM 2022 年 1 月 3 日

1 投票

If you just want to put a title over multiple subplots, you can use sgtitle().
If you want text in a box with arbitrary location, you can use annotations.
[x y z] = peaks(30);
subplot(2,1,1)
surf(x,y,z,'facelighting','flat');
camlight
subplot(2,1,2)
surf(x,y,z,'facelighting','gouraud');
camlight
annstr = sprintf('blah blah\nblah'); % annotation text
annpos = [0.1 0.1 0.1 0.1]; % annotation position in figure coordinates
ha = annotation('textbox',annpos,'string',annstr);
ha.HorizontalAlignment = 'center';
ha.BackgroundColor = [0.9 0.5 1]; % make the box opaque with some color
The other properties of the annotation can be set as needed.

1 件のコメント

ST
ST 2023 年 7 月 1 日
Thanx a lot!. It worked for me

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

その他の回答 (1 件)

dpb
dpb 2022 年 1 月 3 日

0 投票

text() is exclusively a child of an axes, not a figure. To write text outside the boundaries of an axis, use the annotation object instead.
Or, depending upon just what you want/need for this to contain, use tiledlayout with which you can have a shared title and axes labels. Those may or may not be sufficient for your purpose.

カテゴリ

製品

リリース

R2021b

質問済み:

2022 年 1 月 3 日

コメント済み:

ST
2023 年 7 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by