Create text area component
creates a text area in
a new figure window and returns the txa
= uitextareaTextArea
object. MATLAB® calls the uifigure
function to create the
figure.
specifies txa
= uitextarea(___,Name,Value
)TextArea
properties using one or more
Name,Value
pair arguments. Use this option with any of the
input argument combinations in the previous syntaxes.
fig = uifigure; txa = uitextarea(fig);
Create a populated text area.
fig = uifigure; txa = uitextarea(fig,... 'Value', {'First Name Last Name';... 'Address 1'; 'Address 2';'City, State'; 'Postal Code'});
Notice that the text area includes a scroll bar so that the app user can view the postal code.
Determine the current size of the text area.
size = txa.Position(3:4)
size = 150 60
Increase the text area size so that the postal code displays without the use of a scroll bar.
txa.Position(3:4) = [155 75];
Scroll to the bottom of a text area programmatically.
Create a text area. Specify a size and long text for it.
fig = uifigure;
txa = uitextarea(fig);
txa.Position = [100 100 80 80];
txa.Value = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.';
Scroll to the bottom of the text area.
scroll(txa,'bottom')
Create a text area and two labels. When an app user types text and clicks outside the text area, a label thanks the app user for the input. If the app user removes the text and clicks outside the text area, the thank you text is removed.
Save the following code to comments.m
on your MATLAB path. This code creates a figure window containing two labels and
a text area. When an app user types text and clicks outside the text area, the
ValueChangedFcn
callback updates one of the labels to
thank the user.
function comments % Create figure window and components fig = uifigure('Position',[500 500 430 275]); label1 = uilabel(fig,... 'Position',[100 164 100 15],... 'Text','Enter Comments:'); label2 = uilabel(fig,... 'Position',[100 75 175 15],... 'Text',''); textarea = uitextarea(fig,... 'Position',[100 100 150 60],... 'ValueChangedFcn',@(textarea,event) textEntered(textarea, label2)); % Create ValueChangedFcn callback function textEntered(textarea,label2) val = textarea.Value; label2.Text = ''; % Check each element of text area cell array for text for k = 1:length(val) if(~isempty(val{k})) label2.Text = 'Thank you for your comments!'; break; end end end end
Run comments
, and type text in the text area field.
Click outside the text area to trigger the callback.
parent
— Parent containerFigure
object (default) | Panel
object | Tab
object | ButtonGroup
object | GridLayout
objectParent container, specified as a Figure
object created using
the uifigure
function, or one of its child
containers: Tab
, Panel
, ButtonGroup
, or GridLayout
. If you do not specify a parent container,
MATLAB calls the uifigure
function to create a new Figure
object that serves as the parent container.
Specify optional
comma-separated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
'Editable','off'
specifies that the app user cannot
change the text area text.The properties listed here are a subset of the available properties. For the full list, see TextArea Properties.
'Value'
— Value{''}
(default) | character vector | cell array of character vectors | string array | 1-D categorical arrayValue, specified as a character vector, cell array of character vectors, string array, or 1-D categorical array. MATLAB can properly render formatted text, such as this:
cellArrayText{1} = sprintf('%s\n%s', 'Line 1', 'Line 2') cellArrayText{2} = sprintf('%s\n%s', 'Line 3', 'Line 4') textarea = uitextarea('Value',cellArrayText);
If you specify this property as a categorical array, MATLAB uses the values in the array, not the full set of categories.
If the text does not fit into the width of the text area, MATLAB wraps the text.
If there are too many rows to display in the text area, MATLAB adds a scroll bar.
Example: {'Joseph Welford'; 'Mary Reilly'; 'Roberta Silberlicht'}
'ValueChangedFcn'
— Value changed callback''
(default) | function handle | cell array | character vectorValue changed callback, specified as one of these values:
A function handle.
A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.
A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.
The callback executes when the user changes the text and either presses
Tab or clicks outside the text area. It does not execute if the
Value
property changes programmatically.
This callback function can access specific information about the user’s interaction
with the text area. MATLAB passes this information in a ValueChangedData
object as the second argument to your callback function.
In App Designer, the argument is called event
. You can query the
object properties using dot notation. For example,
event.PreviousValue
returns the previous value of the text area.
The ValueChangedData
object is not available to
callback functions specified as character vectors.
The following table lists the properties of the ValueChangedData
object.
Property | Value |
---|---|
Value | Value of text area after app user’s most recent interaction with it |
PreviousValue | Value of text area before app user’s most recent interaction with it |
Source | Component that executes the callback |
EventName | 'ValueChanged' |
For more information about writing callbacks, see Write Callbacks in App Designer.
'Position'
— Location and size of text area[100 100 150 60]
(default) | [left bottom width height]
Location and size of the text area relative to the parent, specified
as the vector [left bottom width height]
. This
table describes each element in the vector.
Element | Description |
---|---|
left | Distance from the inner left edge of the parent container to the outer left edge of the text area |
bottom | Distance from the inner bottom edge of the parent container to the outer bottom edge of the text area |
width | Distance between the right and left outer edges of the text area |
height | Distance between the top and bottom outer edges of the text area |
All measurements are in pixel units.
The Position
values are relative to the
drawable area of the parent container. The drawable area is the area
inside the borders of the container and does not include the area occupied by decorations such
as a menu bar or title.
Example: [100 100 100 90]