Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to insert textbox and write text in a figure by code (not manually)

1,184 views
Skip to first unread message

James Anderson

unread,
Aug 16, 2007, 10:45:34 AM8/16/07
to
Hi,

I want to insert a textbox in a given location of a figure
and write some desciption in it, there is no edge in the
textbox. I know how to do this manually, but don't know how
to do this with script.

Thanks,

James

Scott Frasso

unread,
Aug 16, 2007, 11:35:34 AM8/16/07
to
Hello James,

% Create the figure
mFigure = figure()

% Create a uicontrol of type "text"
mTextBox = uicontrol('style','text')
set(mTextBox,'String','Hello World')

% To move the the Text Box around you can set and get the position of Text
Box itself
mTextBoxPosition = get(mTextBox,'Position')
% The array mTextBoxPosition has four elements
% [x y length height]

% Something that I find useful is to set the Position Units to Characters,
the default is pixels
set(mTextBox,'Units','characters')
% This means a Text Box with 3 lines of text will have a height of 3

-Scott

"James Anderson" <jander...@yahoo.com> wrote in message
news:fa1nue$lhh$1...@fred.mathworks.com...

James Anderson

unread,
Aug 16, 2007, 2:02:00 PM8/16/07
to
Scott,

Thanks for your reply, it works! One additional question is
that how to set the background color of the textbox to be
transparent as if there is no box frame, but still with
strings.

I used the following command:
set(mTextBox,'BackgroundColor',[1 1 1])
but don't know what's the corresponding 3 numbers for
transparent, or I should set some other properties instead
of background to do this?

Many thanks,

James

"Scott Frasso" <scott....@mathworks.com> wrote in message
<fa1qs8$83n$1...@fred.mathworks.com>...

Jeff

unread,
Aug 16, 2007, 2:03:21 PM8/16/07
to
Hey, cool hint. I've also wondered how to add text to plots automatically.

Can you set the position to a subplot index instead of a certain x,y location?

ie:

subplot(2,2,1);
% plot 1

subplot(2,2,2);
% plot 2

subplot(2,2,3);
% plot 3

subplot(2,2,4);
% textbox

us

unread,
Aug 16, 2007, 2:25:37 PM8/16/07
to
Jeff:
<SNIP wants to set his/her subplot axes manually...

> Can you set the position to a subplot index instead of a

certain x,y location...

yes, but with limitations...

% look at this example
pos=[
.1,.1,.3,.3
.4,.4,.3,.3
];
subplot(2,2,[1,2]);
for i=1:size(pos,1)
subplot('position',pos(i,:));
pause
end
% so - a new subplot removes all others underneath it...
% a better solution
% get pos of a subplot
figure;
sh=subplot(2,2,1);
pos=[
get(sh,'position')
pos
];
delete(sh); % not necessary...
ah=zeros(size(pos,1),1);
for i=1:size(pos,1)
ah(i)=axes('position',pos(i,:));
end
axes(ah(1)); % make axis one active...

us

James Anderson

unread,
Aug 17, 2007, 8:47:14 AM8/17/07
to
Scott,

Another question I forget to ask is that in the command:


set(mTextBox,'String','Hello World')

is there any way to set the string to a two-line text so
that the first line is empty and the second line is "hello
world"? I tried several times but seems that it only works
for one line.

Thanks,

James
"Scott Frasso" <scott....@mathworks.com> wrote in message
<fa1qs8$83n$1...@fred.mathworks.com>...

Walter Roberson

unread,
Aug 17, 2007, 10:26:40 AM8/17/07
to
In article <fa45ci$j9b$1...@fred.mathworks.com>,

James Anderson <jander...@yahoo.com> wrote:
>Another question I forget to ask is that in the command:
>set(mTextBox,'String','Hello World')
>is there any way to set the string to a two-line text so
>that the first line is empty and the second line is "hello
>world"? I tried several times but seems that it only works
>for one line.

set(mTextbox,'String',{'', 'Hello World'})

You will also find that in some contexts (e.g., -some- uicontrol
styles), you can use '|' to mark the end of lines

set(controlhandle,'String', '|Hello World');

and in some contexts, you can use \n between lines

set(somehandle, 'String', sprintf('\nHello World') );

--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton

Scott Frasso

unread,
Aug 19, 2007, 10:54:42 AM8/19/07
to
Jeff,

<how to set the background color of the textbox to be
transparent as if there is no box frame, but still with
strings.>

You can set the background color of the Text Box to the same color as the
Figure window by getting the figure windows "Color" value and applying it to
the "BackgroundColor" of the Text Box.

% Get the Color of the figure window
colorOfFigureWindow = get(mFigure,'Color');

%Set the BackgroundColor of the text box
set(mTextBox,'BackgroundColor',colorOfFigureWindow)

-Scott


"James Anderson" <jander...@yahoo.com> wrote in message

news:fa23eo$rtk$1...@fred.mathworks.com...

Shoo

unread,
Nov 23, 2010, 1:02:03 AM11/23/10
to
Hello,

This works for me:
text('Position',[0 1],'string','Hello World')

Thanks

"James Anderson" <jander...@yahoo.com> wrote in message <fa1nue$lhh$1...@fred.mathworks.com>...

Tina

unread,
Oct 27, 2013, 10:45:05 AM10/27/13
to
"Scott Frasso" <scott....@mathworks.com> wrote in message <fa1qs8$83n$1...@fred.mathworks.com>...
Brilliant info for myself. But is there a way to edit the size of the text box?
Thanks in advance
T

Sourjya Sarkar

unread,
Mar 17, 2014, 5:58:10 AM3/17/14
to
"Scott Frasso" <scott....@mathworks.com> wrote in message <fa1qs8$83n$1...@fred.mathworks.com>...
Hi,

Thanks for the codes. It was helpful. I have a few questions

1) How to do I manually insert text into the textbox in a figure without using the 'set' command and read them into a variable? Can this be done for multiple textboxes?

2) How do I set the position of the text box?

Steven Lord

unread,
Mar 17, 2014, 9:39:57 AM3/17/14
to

"Sourjya Sarkar" <sourjy...@gmail.com> wrote in message
news:lg6gvi$p63$1...@newscl01ah.mathworks.com...
> "Scott Frasso" <scott....@mathworks.com> wrote in message
> <fa1qs8$83n$1...@fred.mathworks.com>...

*snip*

> Thanks for the codes. It was helpful. I have a few questions
>
> 1) How to do I manually insert text into the textbox in a figure without
> using the 'set' command and read them into a variable? Can this be done
> for multiple textboxes?
>
> 2) How do I set the position of the text box?

If you're looking to annotate a graph, the GTEXT function may be of interest
to you:

http://www.mathworks.com/help/matlab/ref/gtext.html

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

0 new messages