uieditfield (App Designer)

Create text or numeric edit field component

Use only with App Designer or figures created with the uifigure function. When using GUIDE or the figure function, create an edit field using uicontrol.

Syntax

  • edt = uieditfield
  • edt = uieditfield(style)
  • edt = uieditfield(parent)
    example
  • edt = uieditfield(parent,style)
  • edt = uieditfield(___,Name,Value)
    example

Description

edt = uieditfield creates a text edit field in a new UI figure window and returns the edit field object.

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

example

edt = uieditfield(parent) specifies the object in which to create the edit field. The parent must be a UI figure window, or a tab, panel, or button group within a UI figure window.

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

Create Numeric Edit Field

example

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

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® opens an error tooltip and reverts the value to the last valid value.

Parent object of edit field, specified as a figure, panel, tab, or button group object.

The parent object must be either a figure created using the uifigure function, or a tab, panel, or button group within such a figure.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Each type of edit field object supports a different set of properties. For a full list of properties and descriptions for each type, see the associated property page.

Examples

collapse all

Create a text edit field in a UI figure window.

fig = uifigure;
edt = uieditfield(fig);

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

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

Specify a panel as the parent object for a numeric edit field.

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

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 enter a value in the numeric edit field that is outside the limits, MATLAB displays a message. The message indicates the problem and 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. MATLAB stores the exact value that the app user enters.

fig = uifigure;
edt = uieditfield(fig,'numeric',...
                      'ValueDisplayFormat', '%.2f');

Type 5.5556 in the numeric edit field, and then click outside it. The edit field displays 5.56.

MATLAB stores the value as 5.556. If you click in the edit field, it displays 5.556.

Code the ValueChangedFcn callback for a text edit field 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 UI figure and components.

fig = uifigure('Position',[100 100 350 275]);

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 the ValueChangedFcn callback for a numeric edit field 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 UI figure and components

fig = uifigure('Position',[100 100 350 275]);

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.

Code the ValueChangedFcn callback for a text edit field 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 text edit field callback uses the PreviousValue event data property to populate the text area.

Save the following code to logNames.m on your MATLAB path.

function logNames
% Create UI figure and components

fig = uifigure('Position',[100 100 410 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.

Introduced in R2016a

Was this topic helpful?