Main Content

menu

(不推荐)创建多选择项对话框

    不推荐使用 menu。请改用 listdlg

    说明

    示例

    choice = menu(message,options) 显示一个模态多选择项对话框,其中包含 message 中的文本。options 中的每个元素都显示为一个按钮。该函数返回所选按钮的索引,如果用户点击窗口上的关闭按钮,则返回 0

    示例

    choice = menu(message,opt1,...,optn) 显示一个对话框,其中包含由 opt1optn 指定的选项。

    示例

    全部折叠

    创建一个包含四个选项的多选择项对话框。

    msg = "Choose your favorite animal";
    opts = ["Dog" "Cat" "Horse" "Snake"];
    choice = menu(msg,opts);

    A menu with the message "Choose your favorite animal" at the top and four buttons with the options.

    选择第二个选项并在命令行窗口中显示选择项。

    disp("You chose " + opts(choice))
    You chose Cat

    创建一个多选择项对话框,提示用户选择一种绘图颜色。

    choice = menu("Choose a color","Red","Blue","Green")

    A menu with the message "Choose a color" at the top and three buttons with the color options.

    选择绿色。由于这是第三个菜单选项,该函数返回 3

    choice =
    
         3
    

    将颜色选项存储为一个向量,并使用 choice 的值作为索引提取所选颜色。使用所选绘图颜色绘制一些数据。

    colors = ["r" "b" "g"];
    plotcolor = colors(choice);
    t = 0:.1:60;
    s = sin(t);
    plot(t,s,plotcolor)

    Sinusoidal data plot. The line color is green.

    输入参数

    全部折叠

    对话框文本,指定为字符向量或字符串标量。

    对话框选项,指定为字符向量元胞数组或字符串数组。每个数组元素对应一个单独的按钮。

    对话框选项,指定为字符向量或字符串标量。

    输出参数

    全部折叠

    对话框选择,以非负整数形式返回。当用户选择一个按钮时,函数返回该按钮的索引。例如,如果您将菜单选项指定为 ["Red" "Blue" "Green"],并且用户选择蓝色,则该函数返回 2

    如果用户在选择选项之前关闭对话框,该函数将返回 0

    详细信息

    全部折叠

    模态对话框

    模态对话框阻止用户在响应该对话框之前与其他 MATLAB® 窗口进行交互。

    提示

    • 要从 UIControl 或其他 UI 组件的回调中调用 menu,请将该对象的 Interruptible 属性设置为 "on"。有关详细信息,请参阅 UIControl 属性

    • 在不提供图形功能的终端上,menu 在命令行窗口中将选项显示为编号列表。

    版本历史记录

    在 R2006a 之前推出

    全部折叠

    R2015b: 不推荐使用 menu 函数。

    使用 listdlg 函数创建多选择项对话框。当前没有删除 menu 函数的计划。

    另请参阅

    | | | | |