Wrapped string matrix for given uicontrol


outstring = textwrap(h,instring)
outstring = textwrap(h,instring,columns)
[outstring,position] = textwrap(...)


outstring = textwrap(h,instring) returns a wrapped string cell array, outstring, that fits inside the uicontrol with handle h. instring is a cell array, with each cell containing a single line of text. outstring is the wrapped string matrix in cell array format. Each cell of the input string is considered a paragraph.

outstring = textwrap(h,instring,columns) returns an outstring with each line wrapped at columns characters. Spaces are included in the character count.

[outstring,position] = textwrap(...) returns the recommended position of the uicontrol in the units of the uicontrol. position considers the extent of the multiline text in the x and y directions.

textwrap maintains the original line breaks in the input cell array and adds new ones. It can calculate uicontrol positions with any type of Units, including normalized units.


Place two text-wrapped strings in text uicontrols. The left one has a Position calculated by textwrap in Units of pixels; the right one's Position is calculated manually in Units of characters:

figure('Position',[560 528 350 250]);
% Make a text uicontrol to wrap in Units of Pixels
% Create it in Units of Pixels, 100 wide, 10 high
pos = [10 100 100 10];   
ht = uicontrol('Style','Text','Position',pos);
string = {'This is a string for the left text uicontrol.',...
          'to be wrapped in Units of Pixels,',...
          'with a position determined by TEXTWRAP.'};
% Wrap string, also returning a new position for ht
[outstring,newpos] = textwrap(ht,string) %#ok<NOPRT>

outstring = 
    'This is a string for'
    'the left text'
    'to be wrapped in'
    'Units of Pixels,'
    'with a position'
    'determined by'

newpos =
    10   100    91   124


% Make another text uicontrol to wrap to a column width of 18
colwidth = 18;
% Create it in Units of Pixels, 100 wide, 10 high
pos1 = [150 100 100 10]; 
ht1 = uicontrol('Style','Text','Position',pos1);
string1 = {'This is a string for the right text uicontrol.',...
          'to be wrapped in Units of Characters,',...
          'into lines 18 columns wide.'};
outstring1 = textwrap(ht1,string1,colwidth);
% Reset Units of ht1 to Characters to use the result
newpos1 = get(ht1,'Position')

newpos1 =
   29.8000    7.6154   20.0000    0.7692

% Set new Position in Characters to be specified colwidth
% with height the length of the outstring1 cell array + 1.
newpos1(3) = colwidth; 
newpos1(4) = length(outstring1)+1

newpos1 =
   29.8000    7.6154   15.0000   10.0000


More About

expand all


Do not call copyobj or textwrap (which calls copyobj) inside a CreateFcn. Copying the uicontrol object fires the CreateFcn callback repeatedly, which raises a series of error messages.

See Also


Introduced before R2006a

Was this topic helpful?