Documentation Center

  • Trial Software
  • Product Updates

msgbox

Create and open message dialog box

Syntax

  • h = msgbox(Message) example
  • h = msgbox(Message,Title) example
  • h = msgbox(Message,Title,Icon) example
  • h = msgbox(Message,Title,'custom',IconData,IconCMap) example
  • h = msgbox(___,CreateMode) example

Description

example

h = msgbox(Message) creates a message dialog box that automatically wraps Message to fit an appropriately sized figure.

example

h = msgbox(Message,Title) specifies the title of the message box.

example

h = msgbox(Message,Title,Icon) specifies which built-in icon to display in the message dialog box.

example

h = msgbox(Message,Title,'custom',IconData,IconCMap) specifies a custom icon to include in the message dialog box. IconData is the image data that defines the icon. IconCMap is the colormap used for the image. If IconData is a true-color image, you do not need to specify an IconCMap.

example

h = msgbox(___,CreateMode) specifies whether the message box is modal. Additionally, you can specify a TeX interpreter for Message and Title.

Examples

expand all

Simple Message Dialog Box

Specify the text you want displayed in the message dialog box.

h = msgbox('Operation Completed');

Message Dialog Box Text with Line Breaks

Specify the message dialog box text using a cell array of strings to insert line breaks between the display of each string in the cell array.

h = msgbox({'Operation' 'Completed'});

Message Dialog Box with a Title

Specify the message dialog box text and give the dialog box a title, Success.

h = msgbox('Operation Completed','Success');

Message Dialog Box that Uses a Built-in Icon

Include a built-in error icon with an error message in a message dialog box entitled Error.

h = msgbox('Invalid Value', 'Error','error');

Message Dialog Box that Uses a True-Color Custom Icon

Use a .png file as an icon in your message dialog box. True-color images, such as .png files, do not use colormaps.

Copy a .png file to a folder on your MATLAB® search path.

copyfile(fullfile(matlabroot,...
         'help','includes','product',...
         'images','global','ico_large_info.png'));

Determine the value to specify for IconData by passing the image file to imread.

[cdata] = imread('ico_large_info.png'); 

Create the message dialog box, including the custom icon. Because .png files are true-color, you do not specify a colormap.

h=msgbox('Operation Completed',...
         'Success','custom',cdata);

Message Dialog Box that Uses an Indexed Custom Icon

Use trees.tiff (which is on the MATLAB path) as an icon in your message dialog box. Because .tiff images use a colormap to define the colors, you must specify a colormap. Change the colormap to change the image colors.

Determine the value to specify for IconData by passing the image file to imread.

[cdata,map] = imread('trees.tif'); 

Create the message dialog box, including the custom icon.

h=msgbox('Operation Completed',...
         'Success','custom',cdata,map);

Adjust the image colors by specifying a different colormap. For instance, specify the MATLAB built-in colormap, summer

h=msgbox('Operation Completed','Success','custom',...
          cdata,summer);

Modal Message Dialog Box

Create a modal message dialog box, wrapping the call to msgbox with uiwait to make the message dialog box block MATLAB execution until the user responds to the message dialog box.

uiwait(msgbox('Operation Completed','Success','modal'));

Modal Message Dialog Box that Uses a TeX Formatted Message

Create a structure to specify that the user must click OK before interacting with another window and that MATLAB interpret the message text as TeX format.

CreateStruct.Interpreter = 'tex';
CreateStruct.WindowStyle = 'modal';

Create the message dialog box.

h=msgbox('Z = X^2 + Y^2','Value',CreateStruct);

Input Arguments

expand all

Message — Message dialog box textstring vector | string matrix | cell array

Dialog box text specified as a string vector, string matrix, or cell array.

Example: 'Operation Completed'

Example: ['Operation ', 'Completed']

Example: {'Operation', 'Completed'}

Title — Message dialog box title bar textstring vector

Dialog box title bar text specified as a string vector.

Example: 'Success'

Icon — Icon to include in message dialog box'none' (default) | 'error' | 'help' | 'warn' | 'custom'

Icon to include in message dialog box specified as a string.

Built-in icons appear as follows:

  • Error

  • Help

  • Warn

IconData — Image data defining a custom iconmatrix

Image data defining a custom icon specified as a matrix. Each element of the matrix specifies the color of a rectangular segment in the image. Use imread to get the IconData value for an image that you want to use as a message dialog box icon.

Example: [1:64]'*[1:64])/64

IconCMap — Colormap for a custom icon that is not true-colorm–by–3 matrix | built-in colormap

Colormap for a custom icon that is not true-color, specified as a m-by-3 matrix of real numbers between 0.0 and 1.0, or as a MATLAB built-in colormap. Use imread to get the IconCMap value for an image that you want to use as a message dialog box icon.

Example: [0.5 0.5 0.5]

Example: hot(64)

CreateMode — Message dialog box mode'nonmodal' (default) | structure | 'modal' | 'replace'

Mode in which message dialog box is created, specified as a string or a structure.

  • If CreateMode is a structure, it can have the fields WindowStyle and Interpreter. The WindowStyle field must be one of the strings listed in the list items that follow this one. The Interpreter field must be the string 'tex' or 'none'. If the Interpreter value is 'tex', MATLAB interprets the Message and Title values as TeX. The default value for Interpreter is 'none'.

  • If CreateMode is 'nonmodal', MATLAB creates a new nonmodal message box with the specified parameters. Existing message boxes with the same Title remain.

  • If CreateMode is 'modal', MATLAB replaces the existing message box with the specified Title that was last created or clicked on with the specified modal dialog box. MATLAB deletes all other message boxes with the same title. The replaced message box can be either modal or nonmodal.

  • If CreateMode is 'replace', MATLAB replaces the message box having the specified Title that was last created or clicked on with a nonmodal message box as specified. MATLAB deletes all other message boxes with the same title. The replaced message box can be either modal or nonmodal.

Example: CreateStruct.Interpreter='tex';

Output Arguments

expand all

h — Message dialog box handlescalar

Message dialog box handle returned as a scalar. This is a unique identifier, which you can use to query and modify the properties of a specific message dialog box.

More About

expand all

modal dialog box

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

For more information about modal dialog boxes, see WindowStyle in the Figure Properties topic.

Tips

  • Program execution continues even when a modal dialog box is active. To block MATLAB program execution until the user responds to the modal dialog box, use the uiwait function.

  • If you open a dialog box with errordlg, msgbox, or warndlg using 'CreateMode','modal' and a nonmodal dialog box that was created with any of these functions is already present and has the same title as the modal dialog box, then the nonmodal dialog box closes when the modal one opens.

See Also

| | |

Was this topic helpful?