Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

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

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

From: James Anderson

Date: 16 Aug, 2007 14:45:34

Message: 1 of 8

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

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

From: Scott Frasso

Date: 16 Aug, 2007 15:35:34

Message: 2 of 8

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" <janderson_net@yahoo.com> wrote in message
news:fa1nue$lhh$1@fred.mathworks.com...
> 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


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

From: James Anderson

Date: 16 Aug, 2007 18:02:00

Message: 3 of 8

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.frasso@mathworks.com> wrote in message
<fa1qs8$83n$1@fred.mathworks.com>...
> 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" <janderson_net@yahoo.com> wrote in message
> news:fa1nue$lhh$1@fred.mathworks.com...
> > 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
>
>

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

From: Jeff

Date: 16 Aug, 2007 18:03:21

Message: 4 of 8

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

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

From: us

Date: 16 Aug, 2007 18:25:37

Message: 5 of 8

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

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

From: James Anderson

Date: 17 Aug, 2007 12:47:14

Message: 6 of 8

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.frasso@mathworks.com> wrote in message
<fa1qs8$83n$1@fred.mathworks.com>...
> 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" <janderson_net@yahoo.com> wrote in message
> news:fa1nue$lhh$1@fred.mathworks.com...
> > 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
>
>

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

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 17 Aug, 2007 14:26:40

Message: 7 of 8

In article <fa45ci$j9b$1@fred.mathworks.com>,
James Anderson <janderson_net@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

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

From: Scott Frasso

Date: 19 Aug, 2007 14:54:42

Message: 8 of 8

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" <janderson_net@yahoo.com> wrote in message
news:fa23eo$rtk$1@fred.mathworks.com...
> 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.frasso@mathworks.com> wrote in message
> <fa1qs8$83n$1@fred.mathworks.com>...
>> 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" <janderson_net@yahoo.com> wrote in message
>> news:fa1nue$lhh$1@fred.mathworks.com...
>> > 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
>>
>>
>


Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
graphics us 19 Aug, 2007 11:03:51
axes us 16 Aug, 2007 14:30:23
subplot us 16 Aug, 2007 14:30:23
position us 16 Aug, 2007 14:30:23
code us 16 Aug, 2007 14:30:23
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics