Create date picker component
creates a date picker
in a new figure and returns the d
= uidatepickerDatePicker
object. MATLAB® calls the uifigure
function to create the
figure.
specifies d
= uidatepicker(Name,Value
)DatePicker
property values using one or more
Name,Value
pair arguments.
creates the date picker in the specified container and sets one or more
d
= uidatepicker(parent
,Name,Value
)DatePicker
property values.
Create a date picker in the upper left corner of a figure.
fig = uifigure('Position',[500 500 320 280]); d = uidatepicker(fig,'Position',[18 235 150 22]);
Create a date picker that displays the date in the text field using the
dd-MM-yyyy
format. The watermark in the running app
displays the new format, and all selected dates use that format.
fig = uifigure('Position',[500 500 320 280]); d = uidatepicker(fig,'Position',[18 235 150 22]); d.DisplayFormat = 'dd-MM-yyyy';
Create a date picker that disables Sundays and New Year's day 2018.
fig = uifigure('Position',[500 500 375 280]); d = uidatepicker(fig,'Position',[18 225 150 22]); d.DisabledDaysOfWeek = 1; d.DisabledDates = datetime(2018,1,1);
When you expand the date picker and browse to January 2018, the first day of the year and all Sundays are disabled.
Create a program file called mydateapp.m
that creates a
figure and a date picker with a ValueChangedFcn
callback.
function mydateapp fig = uifigure('Position',[340 400 415 300]); d = uidatepicker(fig,'DisplayFormat','MM-dd-yyyy',... 'Position',[130 190 150 22],... 'Value',datetime(2014,4,9),... 'ValueChangedFcn', @datechange); function datechange (src,event) lastdate = char(event.PreviousValue); newdate = char(event.Value); msg = ['Change date from ' lastdate ' to ' newdate '?']; % Confirm new date selection = uiconfirm(f,msg,'Confirm Date'); if (strcmp(selection,'Cancel')) % Revert to previous selection if cancelled d.Value = event.PreviousValue; end end end
The datechange
function displays a confirmation dialog
box and determines which button the user clicks in that dialog box. The date
picker reverts to the previous date if the user clicks
Cancel.
Run the program, and click a date to see the confirmation dialog box.
mydateapp
parent
— Parent containerFigure
object (default) | Panel
object | Tab
object | ButtonGroup
object | GridLayout
objectParent 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.
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
.
d = uidatepicker('Value',datetime('today'))
creates a
date picker with today's date selected in the UI.Note
The properties listed here are only a subset. For a complete list, see DatePicker Properties.
'Value'
— Selected dateNaT
(default) | datetime
objectSelected date, specified as a datetime
object within the range of the Limits
property. To make the selected date unspecified, set this property to NaT
.
If the specified datetime
object contains time information, only the date information is preserved in the Value
property.
Example: d = uidatepicker('Value',datetime('today'))
Data Types: datetime
'DisplayFormat'
— Display formatDisplay format for the date picker text field, specified as a character vector or string scalar. The default format depends on the locale of the system running the app.
The format you specify must use valid letter identifiers that correspond to the Unicode® Locale Data Markup Language (LDML) standard for dates and times. To separate fields, you can include nonletter characters such as a hyphen, space, colon, or any non-ASCII characters.
Example: d = uidatepicker('DisplayFormat','dd/MM/yy')
This table lists common display formats. The examples show formatted output for the date, Wednesday, April 9, 2014.
Value of Format | Example |
---|---|
'yyyy-MM-dd' | 2014-04-09 |
'dd/MM/yyyy' | 09/04/2014 |
'dd.MM.yyyy' | 09.04.2014 |
'yyyy年 MM月
dd日' | 2014年 04月 09日 |
'MMMM d, yyyy' | April 9, 2014 |
Use these letter identifiers to create a display format. The third column of this table shows output for the date, Wednesday, April 9, 2014.
Letter Identifier | Description | Display |
---|---|---|
G | Era | CE |
y | Year, with no leading zeros. | 2014 |
yy | Year, using last two digits. | 14 |
yyy , yyyy ... | Year, using at least as many digits as there are instances of
'y' | For the year 2014, 'yyy' displays
2014 , while 'yyyyy'
displays 02014 . |
u , uu , ... | ISO year, a single number designating the year. | 2014 |
Q | Quarter, using one digit | 2 |
QQ | Quarter, using two digits | 02 |
QQQ | Quarter, abbreviated | Q2 |
QQQQ | Quarter, full name | 2nd quarter |
M | Month, numerical, using one or two digits | 4 |
MM | Month, numerical, using two digits | 04 |
MMM | Month, abbreviated name | Apr |
MMMM | Month, full name | April |
MMMMM | Month, capitalized first letter | A |
W | Week of the month, using one digit | 2 |
d | Day of the month, using one or two digits | 9 |
dd | Day of the month, using two digits | 09 |
D | Day of the year, using one, two, or three digits | 99 |
DD | Day of the year, using two digits | 99 |
DDD | Day of the year using three digits | 099
|
e | Day of the week, numerical, using one or two digits | 4 , where Sunday is the first day of the
week |
ee | Day of the week, numerical, using two digits | 04 |
eee | Day, abbreviated name | Wed |
eeee | Day, full name | Wednesday |
eeeee | Day, capitalized first letter | W |
Note
The edit field in the running app accepts delimited numeric values,
even when the DisplayFormat
includes words. For
instance, if the month format is specified as
'MMMM'
, the app accepts a numeric month such as
04
, but will display a month name such as
'April'
.
If the user specifies a day-of-year number in the running app, and the format contains identifiers for both the day of year (D
) and Gregorian year (y
), then datetime
might not read the day-of-year number correctly. Use ISO year (u
) in place of y
.
Use one or more u
characters instead of y
characters to represent the year when working with year numbers near zero.
'ValueChangedFcn'
— Value changed function''
(default) | function handle | cell array | character vectorValue changed function, specified as one of the following:
A function handle.
A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.
A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.
The ValueChangedFcn
callback executes when the user changes the date by typing in the text field or by expanding the date picker and selecting a date.
This callback function can access specific information about the user’s interaction with the
date picker. MATLAB passes this information in a ValueChangedData
object as the second argument to your callback function.
In App Designer, the argument is called event
. You can get the object
properties using dot notation. For example, event.PreviousValue
gets
the previously selected date. The ValueChangedData
object is not available to callback functions specified as character vectors.
The following table lists the properties of the ValueChangedData
object.
Property | Value |
---|---|
Value | New selected date |
PreviousValue | Previously selected date |
Source | Component that executes the callback |
EventName | 'ValueChanged' |
The ValueChangedFcn
callback does not execute when the user re-selects or
re-types the currently selected date. The callback also does not execute when the
Value
property changes programmatically.
For more information about creating callbacks in App Designer, see Write Callbacks in App Designer.
'Position'
— Location and size[100 100 150 22]
(default) | [left bottom width height]
Location and size of the collapsed date picker relative to the parent container, specified as a vector of the form [left bottom width height]
. This table describes each element in the vector.
Element | Description |
---|---|
left | Distance from the inner left edge of the parent container to the outer left edge of the date picker |
bottom | Distance from the inner bottom edge of the parent container to the outer bottom edge of the date picker |
width | Distance between the right and left outer edges of the date picker |
height | Distance between the top and bottom outer edges of the date picker |
All measurements are in pixel units.