This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.


Wrap text for uicontrol


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


outstring = textwrap(h,instring) returns a wrapped cell array of character vectors that fits inside the UIControl specified by h. The instring argument is a cell array of character vectors in which each cell contains a single line of text. outstring is the wrapped cell array of character vectors. Each cell of instring is considered a paragraph.

outstring = textwrap(h,instring,columns) returns outstring with each line wrapped at the number of columns specified by the columns argument. Spaces are included in the character count.

outstring = textwrap(instring,columns) returns a wrapped cell array of character vectors for a specific instring and number of columns.

[outstring,position] = textwrap(...) returns the recommended position of the UIControl in the units of the UIControl. The position output argument 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 wrapped text in two UIControl text boxes. The position of the left text box is calculated by textwrap in pixel units. The position of the right text box is calculated in character units.

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);
txt = {'This is the left text box.',...
          'This text is wrapped in pixel units',...
          'with a position determined by TEXTWRAP.'};
% Wrap text, also returning a new position for ht
[outstring,newpos] = textwrap(ht,txt);


% Make another text uicontrol to wrap to a column width of 20
colwidth = 20;
% Create it in Units of Pixels, 100 wide, 10 high
pos1 = [150 100 100 10]; 
ht1 = uicontrol('Style','Text','Position',pos1);
txt1 = {'This is the right text box.',...
          'This text wrapped in character units',...
          'using a column width of 20.'};
outstring1 = textwrap(ht1,txt1,colwidth);
% Reset Units of ht1 to Characters to use the result
newpos1 = get(ht1,'Position')

% 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



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?