Main Content

menu

(Not recommended) Create multiple-choice dialog box

    menu is not recommended. Use listdlg instead.

    Description

    example

    choice = menu(message,options) displays a modal multiple choice dialog box containing the text in message. Each element in options appears as a button. The function returns the index of the selected button, or 0 if the user clicks the close button on the window.

    example

    choice = menu(message,opt1,...,optn) displays a dialog box with the options specified by opt1 through optn.

    Examples

    collapse all

    Create a multiple choice dialog box with four options.

    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.

    Select the second option and display the choice in the Command Window.

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

    Create a multiple choice dialog box that prompts the user to choose a plot color.

    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.

    Select Green. Because this is the third menu option, the function returns 3.

    choice =
    
         3
    

    Store the color options as a vector, and extract the selected color using the value of choice as the index. Plot some data using the selected plot color.

    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.

    Input Arguments

    collapse all

    Dialog box text, specified as a character vector or a string scalar.

    Dialog box options, specified as a cell array of character vectors or a string array. Each array element corresponds to a separate button.

    Dialog box option, specified as a character vector or a string scalar.

    Output Arguments

    collapse all

    Dialog box selection, returned as a nonnegative integer. When the user selects a button, the function returns the index of that button. For example, if you specify the menu options as ["Red" "Blue" "Green"] and the user selects Blue, the function returns 2.

    If the user closes the dialog box before selecting an option, the function returns 0.

    More About

    collapse all

    Modal Dialog Box

    A modal dialog box prevents a user from interacting with other MATLAB® windows before responding to the dialog box.

    Tips

    • To call menu from within a callback of a UIControl or other UI component, set that object's Interruptible property to "on". For more information, see UIControl Properties.

    • On a terminal that does not provide a graphics capability, menu displays the options as a numbered list in the Command Window.

    Version History

    Introduced before R2006a

    collapse all

    R2015b: The menu function is not recommended

    Use the listdlg function to create multiple choice dialog boxes. There are no plans to remove the menu function at this time.