uieditfield
Create text or numeric edit field component
Syntax
Description
creates a text edit
field in a new figure window and returns the edt
= uieditfieldEditField
object. MATLAB® calls the uifigure
function to create the
figure.
creates an edit field of the specified style.edt
= uieditfield(style
)
specifies object properties using one or more name-value arguments. Use this option
with any of the input argument combinations in the previous syntaxes.edt
= uieditfield(___,Name,Value
)
Examples
Create Text Edit Field
Create a text edit field in a window.
fig = uifigure; edt = uieditfield(fig);
Create Numeric Edit Field
Create a numeric edit field by specifying the style as numeric.
fig = uifigure;
edt = uieditfield(fig,"numeric");
Create Numeric Edit Field Within Panel
Specify a Panel
as the
parent object.
fig = uifigure;
pnl = uipanel(fig);
edt = uieditfield(pnl,"numeric");
Set and Access Numeric Edit Field Property Values
Create a numeric edit field with rounding on.
fig = uifigure; edt = uieditfield(fig,"numeric", ... "RoundFractionalValues","on");
Determine the default limits.
limits = edt.Limits
limits = -Inf Inf
The returned values indicate that there are no limits.
Change the limits to 0 through 100. (By default limits are inclusive.)
edt.Limits = [0 100];
Specify Numeric Edit Field Limit Inclusiveness
Create a numeric edit field that allows the app user to enter a value greater than -5 and less than or equal to 10.
fig = uifigure; edt = uieditfield(fig,"numeric", ... "Limits",[-5 10], ... "LowerLimitInclusive","off", ... "UpperLimitInclusive","on", ... "Value",5);
If you type a value in the numeric edit field that is outside the limits, MATLAB displays a message that indicates the problem. If you enter the invalid value, MATLAB restores the value to the previous valid value.
Specify Numeric Edit Field Display Format
Create a numeric edit field that allows the app user to enter any value, but always displays the value using exactly two decimal places and the specified units. MATLAB stores the exact value that the app user enters.
fig = uifigure; edt = uieditfield(fig,"numeric", ... "ValueDisplayFormat","%.2f Volts");
Type 5.5556
in the numeric edit field, and then click
outside it. The edit field displays 5.56 Volts
.
MATLAB stores the value as 5.5556
. If you click in
the edit field again, it displays 5.5556
. For a complete
list of supported format display operators, see sprintf
.
Specify Length and Type of Edit Field Text
Create a text edit field that allows the app user to enter text that is between 3 and 12 characters long and that consists only of letters and digits.
fig = uifigure; edt = uieditfield(fig, ... "CharacterLimits",[3 12], ... "InputType","alphanumerics");
If you type a value in the text edit field that is invalid, MATLAB displays a message that indicates the problem. If you then enter the invalid value by pressing Enter or navigating away from the component, MATLAB restores the value to the previous valid value.
Code Response to Changed Edit Field Text
Code the ValueChangedFcn
callback so that
when the app user changes text in the edit field, a label is updated to match
that text.
Save the following code to textValue.m
on your
MATLAB path.
function textValue % Create figure and components. fig = uifigure("Position",[100 100 366 270]); lbl = uilabel(fig,... "Position",[130 100 100 15]); txt = uieditfield(fig,... "Position",[100 175 100 22],... "ValueChangedFcn",@(txt,event) textChanged(txt,lbl)); end % Code the callback function. function textChanged(txt,lbl) lbl.Text = txt.Value; end
Run textValue
, and type Velocity
in
the edit field. Click outside the edit field to trigger the callback.
Code Response to Changed Numeric Edit Field Value
Code the ValueChangedFcn
callback such
that when the app user changes the value in the edit field, a slider is updated
to match that value.
Save the following code to numericEditFieldValue.m
on
your MATLAB path.
function numericEditFieldValue % Create figure and components fig = uifigure("Position",[100 100 366 270]); slider = uislider(fig,... "Position",[100 140 120 3]); numfld = uieditfield(fig,"numeric",... "Position",[110 200 100 22],... "ValueChangedFcn",@(numfld,event) numberChanged(numfld,slider)); end % Create ValueChangedFcn callback function numberChanged(numfld,slider) slider.Value = numfld.Value; end
Run numericEditFieldValue
.
Enter a value from 0 to 100 in the numeric edit field and click outside the field. The slider moves to indicate the numeric edit field value.
Use Event Data to Maintain a Log
Code the ValueChangedFcn
callback to
maintain a log of values entered in a single session. When the app user changes
the value in the edit field, the previous field value is added to a list
maintained in a text area. The callback uses the
PreviousValue
property returned in the
event
argument to populate the text area.
Save the following code to logNames.m
on your
MATLAB path.
function logNames % Create figure and components fig = uifigure("Position",[100 100 366 400]); loglist = uitextarea(fig,... "Position",[134 49 150 277],... "Editable","off"); namefld = uieditfield(fig,"text",... "Value", "Bob Langley",... "Position",[134 367 100 22],... "ValueChangedFcn",@(namefld,event) nValChanged(namefld,event,loglist)); end % Create ValueChangedFcn callback function nValChanged(namefld,event,loglist) newvalue = event.Value; previousValue = event.PreviousValue; loglist.Value = [previousValue; loglist.Value]; end
Run logNames
.
Each time you enter a name in the text edit field and press enter, the name that was previously in the text edit field is added to the text area.
Input Arguments
style
— Type of edit field
"text"
(default) | "numeric"
Type of edit field, specified as one of the following:
"text"
By default, text edit fields are empty.
"numeric"
By default, numeric edit fields display the value 0. If the app user types a nonnumeric value in a numeric edit field, MATLAB displays an error tooltip and reverts the value to the previous valid value.
parent
— Parent container
Figure
object (default) | Tab
object | Panel
object | ButtonGroup
object | GridLayout
object
Parent 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.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
EditField
and NumericEditField
objects support different sets of properties. For a
full list of properties and descriptions for each object, see the associated
property page.
EditField Properties — If
style
is"text"
(default)NumericEditField Properties — If
style
is"numeric"
Version History
Introduced in R2016aR2022b: Specify valid length and input type for edit field text
Specify input constraints for text edit fields.
Use the
CharacterLimits
property to specify a maximum and minimum number of allowed characters.Use the
InputType
property to restrict the allowed character types.
For more information, see EditField Properties.
R2021a: Specify placeholder text
Provide a short hint that describes the expected edit field input by using the
Placeholder
property.
For more information, see EditField Properties.
See Also
Functions
Properties
Tools
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)