主要内容

uidropdown

Create drop-down component

描述

dd= uidropdown创建一个下拉的new figure window and returns the落下目的。MATLAB®calls theuifigure创建图形的功能。

example

dd= uidropdown()在指定的父容器中创建下拉菜单。父母可以是数字使用uifigure功能,或它的一个子容器之一。

example

dd= uidropdown(___,姓名,Value)使用一个或多个指定对象属性姓名,Value配对参数。将此选项与上一个语法中的任何输入参数组合一起使用。使用姓名,Value一对,可编辑,'on'要指定允许应用程序用户将文本输入下拉组件或选择预定义的选项的下拉组件。

例子

collapse all

使用默认项目创建一个下拉组件。

无花果= uifigure;dd = uidropdown(图);

UI figure window with a drop-down component. The drop-down has four options, labeled Option 1 through Option 4.

单击下拉组件中的任何地方都会使其打开。

创建一个下拉组件并指定选项。

无花果= uifigure;dd = uidropdown(图,'项目',{'Red','黄色','蓝色的','绿色的'},,...'Value','蓝色的');

UI图带有下拉组件。下拉曲的值是蓝色的。

确定与所选选项关联的值。

瓦尔ue = dd.Value
值='蓝色'

默认情况下,项目数据property is empty, so the drop-down component value corresponds to the element selected in the drop-down component.

将数据值与每个下拉组件项目相关联。

dd.itemsdata = [1 2 3 4];

确定与所选选项关联的值。

瓦尔ue = dd.Value
值= 3

请注意,当项目数据属性值不是空的,下拉组件的值是项目数据瓦尔ue that corresponds to the selected项目值元素。

无花果= uifigure;dd = uidropdown(图,“可编辑”,'on');

UI图窗口带有值1的可编辑下拉组件。下拉菜单在文本末尾具有光标。

单击下拉组件中的任何位置(除弃箭)以外,插入光标,使用户能够在下拉式组件中键入文本。

创建一个图和下拉组件。当应用程序用户从下拉式组件中进行选择时,绘图会更改颜色。

Save the following code toplotoptions.m在您的Matlab路径上。此代码创建一个包含图和下拉组件的窗口。当应用程序用户更改下拉组件选择时Valuechangedfcncallback changes the plot color.

功能plotOptions fig = uifigure; fig.Position(3:4) = [440 320]; ax = uiaxes(“父母”,fig,...'位置',[10 10 300 300]); x = linspace(-2*pi,2*pi); y = sin(x); p = plot(ax,x,y); p.Color ='蓝色的';dd = uidropdown(图,...'位置',[320 160 100 22],,...'项目',{'Red','黄色','蓝色的','绿色的'},,...'Value','蓝色的',...'Valuechangedfcn',@(dd,event)选择(dd,p));end%创建ValueChangedFCN回调:功能selection(dd,p) val = dd.Value; p.Color = val;end

plotOptions。选择绿色from the drop-down component to change the plot color to green.

UI图窗口带有一个绘图和下拉窗口。该图包含一些带有绿色线颜色的数据,所选的下拉选项为绿色。

Create a drop-down component and a lamp. When the app user makes a selection from the drop-down component, the lamp size changes.

将以下代码保存到Lampsize.m在您的Matlab路径上。This code creates a figure window containing a drop-down component and a lamp. When an app user changes the drop-down component selection, theValuechangedfcn回调会更改灯的大小。

功能灯泡%创建图形和组件无花果= uifigure('位置',[100 100 300 275]);lmp = uilamp(图,...'位置',[100 30 20 20]); dd = uidropdown(fig,...“可编辑”,'on',...'位置',[84 204 100 20],...'项目',{'大小x 1','大小x 2','大小x 3','大小x 4'},,...'itempata',[1 2 3 4],...'Value',1,...'Valuechangedfcn',@(dd,event)optionsElected(dd,lmp));end%创建valuechangedfcn回调功能optionSelected(dd,lmp) val = dd.Value; s = [20 20];转变瓦尔案子{1,2,3,4}% User selected a defined option尺寸= val * s;lmp.position(3:4)=大小;否则%用户输入值m = str2num(val);尺寸= m * s;lmp.position(3:4)=大小;endend

灯泡并从下拉组件中选择各种选项。

Type a value in the drop-down component and press进入。灯尺寸变化。(如果输入一个大价值,则可能必须调整数字大小才能看到灯。)

UI figure window with an editable drop-down above a large lamp. The value in the drop-down is 6.

Input Arguments

collapse all

父容器, specified as a数字使用uifigure功能,或它的一个子容器之一:标签,控制板,屁股群, 或者网格布局。如果您不指定父容器,则MATLAB调用uifigure功能to create a new数字用作父容器的对象。

姓名-Value Arguments

将可选的参数对name1 = value1,...,namen = valuen, 在哪里姓名is the argument name and价值是相应的值。名称值参数必须在其他参数之后出现,但是对的顺序并不重要。

在R2021a之前,请使用逗号分隔每个名称和值,并附上姓名用引号。

例子:'项目',{''','Yellow','Blue'}specifies the options presented in the drop-down component.

此处列出的属性是可用属性的子集。有关完整列表,请参阅下拉属性

值,指定为项目或者项目数据数组。默认,价值is the first element in项目

Specifying价值作为一个要素项目选择与该元素匹配的下拉项。如果项目数据不是空的,然后价值必须设置为项目数据, 和the drop-down will select the associated item in the list.

什么时候可编辑被设定为'on',您可以另外指定价值作为字符向量或字符串标量。

下拉项,指定为字符向量,字符串数组或1-D分类数组的单元格数组。允许重复元素。下拉组件显示的选项与此选项一样多项目array. If you specify this property as a categorical array, MATLAB uses the values in the array, not the full set of categories.

例子:{'红色','Yellow','蓝色'}

例子:{'1','2','3'}

Data associated with each element of the项目适当的价值, specified as a 1-by-n numeric array or a 1-by-n cell array. Duplicate elements are allowed.

例如,如果您设置项目对员工姓名的价值,您可能会设置项目数据对相应的员工ID号的价值。这项目数据应用程序用户不可见值。

如果数组元素中的数量项目数据价值和项目瓦尔ue do not match, one of the following occurs:

  • 当。。。的时候项目数据值是空的,然后是所有元素项目值向应用程序用户提出。

  • 当。。。的时候项目数据价值比项目价值,然后项目值向应用程序用户提出。MATLABignores the extra项目数据元素。

  • 当。。。的时候项目数据瓦尔ue is not empty, but has fewer elements than the项目价值,唯一的要素项目瓦尔ue presented to the app user are those that have a corresponding element in the项目数据价值。

例子:{'One','Two','Three'}

例子:[10 20 30 40]

下拉组件的可编辑状态,指定为'离开'或者'on',或数字或逻辑1(真的) or0(false)。一个值'on'相当于真的, 和'离开'相当于false。Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of typematlab.lang.onoffswitchstate

如果the使能够属性值是'离开',那么应用程序用户也无法更改下拉组件文本,即使可编辑的属性值为'on'

值更改了回调, specified as one of these values:

  • 功能句柄。

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

当用户从下拉列表中选择其他选项时,此回调函数将执行。如果是价值属性以编程方式更改。

此回调函数可以访问有关用户与下拉访问的交互的特定信息。Matlab将此信息传递到Valuechangeddataobject as the second argument to your callback function. In App Designer, the argument is called事件。You can query the object properties using dot notation. For example,event.previousvalue返回下拉下的上一个值。这Valuechangeddataobject is not available to callback functions specified as character vectors.

这following table lists the properties of theValuechangeddata目的。

财产 价值
价值 App用户与其最新交互后的下拉组件值。
上一个值 在App用户与IT进行最新交互之前,下拉式组件值。
编辑

逻辑值指示是否由于将新值键入下拉组件键入而执行回调。

  • 0(false) - 选择或键入的应用程序用户项目下拉组件的属性。

  • 1(真的) — The app user typed a value that is not an element of the项目下拉组件的属性。

资源 执行回调的组件。
EventName “价值”

For more information about writing callbacks, see应用程序设计师中的回调

下拉菜单打开回调函数,指定为以下值之一:

  • 功能句柄。

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

此属性指定一个回调函数,当用户单击以打开下拉菜单时执行。此回调的可能用途是动态更新下拉菜单中的条目列表。

此回调函数可以访问有关用户与下拉访问的交互的特定信息。Matlab将此信息传递到下拉式上的dataobject as the second argument to your callback function. In App Designer, the argument is called事件。You can query the object properties using dot notation. For example,event.sourcereturns the落下用户与用户进行交互以触发回调的对象。这下拉式上的dataobject is not available to callback functions specified as character vectors.

这following table lists the properties of the下拉式上的data目的。

财产 价值
资源 执行回调的组件
EventName “粪便”

For more information about writing callbacks, see应用程序设计师中的回调

下拉组件相对于父的位置和大小,指定为向量[左底宽度高度]。This table describes each element in the vector.

元素 描述
left Distance from the inner left edge of the parent container to the outer left edge of the drop-down component
底部 Distance from the inner bottom edge of the parent container to the outer bottom edge of the drop-down component
宽度 下拉组件的左右边缘之间的距离
高度 Distance between the top and bottom outer edges of the drop-down component

All measurements are in pixel units.

Position值相对于drawable areaof the parent container. The drawable area is the area inside the borders of the container and does not include the area occupied by decorations such as a menu bar or title.

例子:[100 100 100 22]

Version History

Introduced in R2016a

See Also

Functions

特性