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 pair 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);

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

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

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

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

Introduced in R2016a