Main Content

uieditfield

Create text or numeric edit field component

Description

edt = uieditfield creates a text edit field in a new figure window and returns the EditField object. MATLAB® calls the uifigure function to create the figure.

edt = uieditfield(style) creates an edit field of the specified style.

example

edt = uieditfield(parent) creates the edit field in the specified parent container. The parent can be a Figure created using the uifigure function, or one of its child containers.

example

edt = uieditfield(parent,style) creates an edit field of the specified style in the specified parent container.

example

edt = uieditfield(___,Name,Value) specifies object properties using one or more name-value arguments. Use this option with any of the input argument combinations in the previous syntaxes.

Examples

collapse all

Create a text edit field in a window.

fig = uifigure;
edt = uieditfield(fig);

Figure window with a blank edit field.

Create a numeric edit field by specifying the style as numeric.

fig = uifigure;
edt = uieditfield(fig,"numeric");

Figure window with a numeric edit field. The edit field value is 0.

Specify a Panel as the parent object.

fig = uifigure;
pnl = uipanel(fig);
edt = uieditfield(pnl,"numeric");

A numeric edit field in a panel container.

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];

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.

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.

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.

Text edit field. The text in the field is "ab", and the edit field has a red border and an error tooltip with text "Value must be between 3 and 12 characters long."

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.

An app with an edit field and a label. Both the edit field and the label display the text "Velocity".

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.

An app with an edit field and a slider. The text in the edit field matches the value of the slider.

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.

An app with an edit field and a text area. The edit field contains a name, and the text area has a list of multiple names.

Input Arguments

collapse all

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 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.

Version History

Introduced in R2016a

expand all