Documentation

This is machine translation

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

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

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)
edt = uieditfield(parent,style)
edt = uieditfield(___,Name,Value)

Description

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

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 container can be a Figure created using the uifigure function, or one of its child containers: Tab, Panel, or ButtonGroup.

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

Create Numeric Edit Field

example

edt = uieditfield(___,Name,Value) specifies object 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, specified as a Figure created using the uifigure function, or one of its child containers: Tab, Panel, or ButtonGroup.

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.

EditField and NumericEditField objects supports different sets of properties. For a full list of properties and descriptions for object, see the associated property page.

Examples

collapse all

Create a text edit field in a 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.

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