Main Content

msgbox

创建消息对话框

说明

示例

注意

在 App 设计工具和使用 uifigure 函数创建的 App 中,推荐使用 uialert 而不是 msgbox,因为它提供额外的自定义选项。

f = msgbox(message) 创建一个消息对话框,该对话框可将 message 自动换行,以适应合适大小的图窗。

如果您要在多个 App 窗口、MATLAB® 桌面或 Simulink® 上显示消息对话框,并且在响应对话框之前仍能与它们进行交互,请使用此函数。

示例

f = msgbox(message,title) 指定消息框的标题。

示例

f = msgbox(message,title,icon) 指定要在消息对话框中显示的预定义图标。

示例

f = msgbox(message,title,"custom",icondata,iconcmap) 指定要包括在消息对话框中的一个自定义图标。icondata 是定义该图标的图像数据。iconcmap 是用于该图像的颜色图。如果 icondata 是真彩色图像数组,则不需要指定 iconcmap

示例

f = msgbox(___,createmode) 指定对话框的窗口模式。您也可以指定一个结构体数组,该数组指定窗口模式以及 message 的解释器。

示例

全部折叠

指定您想要在消息对话框显示的文本。

f = msgbox("Operation Completed");

Message dialog box with the text "Operation Completed" and an OK button at the bottom

使用字符串数组指定消息对话框文本。

f = msgbox(["Operation";"Completed"]);

Message dialog box with the text "Operation Completed" split over two lines and an OK button at the bottom

指定消息对话框文本并将对话框标题指定为 Success

f = msgbox("The operation has been successfully completed","Success");

Message dialog box with the text "The operation has been successfully completed" and an OK button at the bottom. The dialog box title is "Success".

在标题为“错误”的对话框中包含一个内置错误图标和一条错误消息。

f = msgbox("Invalid Value","Error","error");

Message dialog box with the title "Error" and the text "Invalid Value". The dialog box contains a red error icon to the left of the text.

将 RGB 图像读取到工作区。然后,在对话框中将其指定为一个自定义图标。

myicon = imread("landOcean.jpg");
h = msgbox("Operation Completed","Success","custom",myicon);

Message dialog box with the title "Success" and the text "Operation Completed". The dialog box contains an icon of a world map to the left of the text.

将图像数据和颜色图读取到工作区中。

[icondata,iconcmap] = imread("trees.tif"); 

创建包含自定义图标的消息对话框。

h = msgbox("Operation Completed", ...
         "Success","custom",icondata,iconcmap);

Message dialog box with the title "Success" and the text "Operation Completed". The dialog box contains an icon of a tree to the left of the text.

通过指定不同的颜色图调整图像颜色。例如,指定 MATLAB 内置颜色图 summer

h = msgbox("Operation Completed","Success","custom", ...
          icondata,summer);

Message dialog box with the title "Success" and the text "Operation Completed". The dialog contains an icon of a tree using the green-and-yellow summer colormap to the left of the text.

创建一个模态消息框,使用 uiwait 来控制对 msgbox 的调用,以阻止 MATLAB 执行,直到用户响应消息对话框为止。

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

Message dialog box with the title "Success", the text "Operation Completed", and an OK button at the bottom

创建一个结构体,指定用户必须先点击确定然后才能与其他窗口交互,而且 MATLAB 按 TeX 格式解释消息文本。

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

创建消息对话框。

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

Message dialog box with the title "Value" and a formatted equation with superscripts

输入参数

全部折叠

对话框文本,指定为字符向量、字符向量元胞数组或字符串数组。

示例: "Operation Completed"

示例: ["Operation";"Completed"]

标题,指定为字符向量或字符串标量。

示例: "Success"

图标,指定为 "help""warn""error""none"

下表列出了值和对应的图标。

图标
"help"

Square blue icon with a lowercase "i" character

"warn"

Triangular yellow icon with an exclamation point

"error"

Octagonal red icon with an exclamation point

"none"不显示任何图标。

图像数组,指定为 m×n 数组或 m×n×3 真彩色图像数组。您也可以使用 imread 从文件中获取图像数组。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

颜色图,指定为 RGB 三元组的三列矩阵。RGB 三元组是包含三个元素的行向量,其元素分别指定颜色的红、绿、蓝分量的强度。强度必须在 [0, 1] 范围内。

数据类型: single | double

窗口模式,指定为 "non-modal""modal""replace" 或结构体数组。

  • 如果 createmode"non-modal",MATLAB 会使用指定参数创建一个新的非模态消息框。具有相同 title 的现有消息框将继续保留。

  • 如果 createmode"modal",MATLAB 会用指定的模态对话框替换掉最近创建的或点击过的、具有指定 title 的现有消息框。MATLAB 删除其他所有具有相同标题的消息框。被替代的消息框可以是模态消息框,也可以是非模态消息框。

  • 如果 createmode"replace",MATLAB 会用指定的非模态消息框替换掉最近创建的或点击过的、具有指定 title 的消息框。MATLAB 删除其他所有具有相同标题的消息框。被替代的消息框可以是模态消息框,也可以是非模态消息框。

  • 如果 createmode 是结构体数组,则它必须具有 WindowStyleInterpreter 字段。WindowStyle 字段的值必须为 "non-modal""modal""replace"Interpreter 字段的值必须为 "tex""none"。如果 Interpreter 值是 "tex",MATLAB 会将 message 值解释为 TeX。Interpreter 的默认值是 "none"

TeX 标记

默认情况下,MATLAB 支持一部分 TeX 标记。使用 TeX 标记可添加下标和上标,修改字体类型和颜色,并在文本中包括特殊字符。

修饰符会一直作用到文本结尾,但上标和下标除外,因为它们仅修饰下一个字符或花括号中的字符。当您将解释器设置为 'tex' 时,支持的修饰符如下所示。

修饰符描述示例
^{ }上标'text^{superscript}'
_{ }下标'text_{subscript}'
\bf粗体'\bf text'
\it斜体'\it text'
\sl伪斜体(通常与斜体相同)'\sl text'
\rm常规字体'\rm text'
\fontname{specifier}字体名称 - 将 specifier 替换为字体系列的名称。您可以将此说明符与其他修饰符结合使用。'\fontname{Courier} text'
\fontsize{specifier}字体大小 - 将 specifier 替换为以磅为单位的数值标量值。'\fontsize{15} text'
\color{specifier}字体颜色 - 将 specifier 替换为以下颜色之一:redgreenyellowmagentablueblackwhitegraydarkGreenorangelightBlue'\color{magenta} text'
\color[rgb]{specifier}自定义字体颜色 - 将 specifier 替换为三元素 RGB 三元组。'\color[rgb]{0,0.5,0.5} text'

下表列出了 'tex' 解释器所支持的特殊字符。

字符序列符号字符序列符号字符序列符号

\alpha

α

\upsilon

υ

\sim

~

\angle

\phi

ϕ

\leq

\ast

*

\chi

χ

\infty

\beta

β

\psi

ψ

\clubsuit

\gamma

γ

\omega

ω

\diamondsuit

\delta

δ

\Gamma

Γ

\heartsuit

\epsilon

ϵ

\Delta

Δ

\spadesuit

\zeta

ζ

\Theta

Θ

\leftrightarrow

\eta

η

\Lambda

Λ

\leftarrow

\theta

θ

\Xi

Ξ

\Leftarrow

\vartheta

ϑ

\Pi

Π

\uparrow

\iota

ι

\Sigma

Σ

\rightarrow

\kappa

κ

\Upsilon

ϒ

\Rightarrow

\lambda

λ

\Phi

Φ

\downarrow

\mu

µ

\Psi

Ψ

\circ

º

\nu

ν

\Omega

Ω

\pm

±

\xi

ξ

\forall

\geq

\pi

π

\exists

\propto

\rho

ρ

\ni

\partial

\sigma

σ

\cong

\bullet

\varsigma

ς

\approx

\div

÷

\tau

τ

\Re

\neq

\equiv

\oplus

\aleph

\Im

\cup

\wp

\otimes

\subseteq

\oslash

\cap

\in

\supseteq

\supset

\lceil

\subset

\int

\cdot

·

\o

ο

\rfloor

\neg

¬

\nabla

\lfloor

\times

x

\ldots

...

\perp

\surd

\prime

´

\wedge

\varpi

ϖ

\0

\rceil

\rangle

\mid

|

\vee

\langle

\copyright

©

输出参数

全部折叠

对话框的 Figure 对象。使用 f 来查询和修改对话框的属性。

详细信息

全部折叠

模态对话框

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

非模态对话框

非模态对话框允许用户在响应该对话框之前与其他 MATLAB 窗口进行交互。非模态对话框也称为普通对话框。

提示

  • 模态对话框(使用 errordlgmsgboxwarndlg 创建)会替代使用这些具有相同名称的函数创建的任何现有对话框。

  • 即使有一个模态对话框处于活动状态,程序还会继续执行。要阻止 MATLAB 程序执行,直到用户对模态对话框做出响应,可使用 uiwait 函数。

  • 要为单窗口 App 设计工具或基于 uifigure 的 App 创建模态警报对话框,请改用 uialert 函数。

版本历史记录

在 R2006a 之前推出

另请参阅

| | |