Tags: code, desciption, edge, figure, figureand, insert, location, manually, matlab, programming, text, textbox, thetextbox, write
how to insert textbox and write text in a figure by code (not manually)
On Programmer » Matlab
6,818 words with 6 Comments; publish: Mon, 28 Apr 2008 00:10:00 GMT; (20078.13, « »)
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
http://matlab.itags.org/q_matlab_27218.html
All Comments
Leave a comment...
- 6 Comments

- 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.matlab.itags.org.yahoo.com> wrote in message
news:fa1nue$lhh$1.matlab.itags.org.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
#1; Mon, 28 Apr 2008 00:11:00 GMT

- 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.matlab.itags.org.mathworks.com> wrote in message
<fa1qs8$83n$1.matlab.itags.org.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.matlab.itags.org.yahoo.com> wrote in message
> news:fa1nue$lhh$1.matlab.itags.org.fred.mathworks.com...
>
#2; Mon, 28 Apr 2008 00:12:00 GMT

- Hey, 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 locatio
n?
ie:
subplot(2,2,1);
% plot 1
subplot(2,2,2);
% plot 2
subplot(2,2,3);
% plot 3
subplot(2,2,4);
% textbox
#3; Mon, 28 Apr 2008 00:13:00 GMT

- 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
#4; Mon, 28 Apr 2008 00:14:00 GMT

- 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.matlab.itags.org.mathworks.com> wrote in message
<fa1qs8$83n$1.matlab.itags.org.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.matlab.itags.org.yahoo.com> wrote in message
> news:fa1nue$lhh$1.matlab.itags.org.fred.mathworks.com...
>
#5; Mon, 28 Apr 2008 00:15:00 GMT

- In article <fa45ci$j9b$1.matlab.itags.org.fred.mathworks.com>,
James Anderson <janderson_net.matlab.itags.org.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
#6; Mon, 28 Apr 2008 00:16:00 GMT