Main Content

uialert

显示警报对话框

说明

示例

uialert(fig,message,title) 在指定图窗窗口的前面显示一个模态对话框。图窗必须使用 uifigure 函数创建。此对话框中显示指定的消息,并在标题栏中显示指定的标题。默认情况下,此对话框还包含一个错误图标和一个确定按钮。

示例

uialert(___,Name,Value) 显示一个对话框,此对话框的属性由一个或多个 Name,Value 参量指定。

示例

全部折叠

创建一个模态警告对话框。在您关闭此对话框后,才能访问后面的图窗。

fig = uifigure;
uialert(fig,"File not found.","Invalid File");

Alert dialog box. The icon is a red octagon with an exclamation point. The title of the dialog box is "Invalid File" and the text is "File not found". The box has an OK button in the bottom right.

fig = uifigure;
message = ["Fire hazard!","Consider reducing temperature."];
uialert(fig,message,"Warning", ...
    "Icon","warning");

Warning dialog box. The text spans two lines. The first line says "Fire hazard!" and the second line says "Consider reducing temperature".

创建一个图窗和两个不同大小的矩阵。

fig = uifigure; 
A = rand(3);
B = ones(5);

尝试垂直串联矩阵。通过获取错误文本并在图窗中创建一个警报对话框来处理出现的任何错误,然后将错误文本指定为对话框消息。MATLAB® 错误文本通常包含 HTML 格式,因此请将对话框解释器指定为 'html'。显示的错误所带链接指向 vertcat 函数的文档。

try 
    C = vertcat(A,B);
catch ME 
    report = getReport(ME);
    uialert(fig,report,"Error Message","Interpreter","html");
end

Alert dialog box. The message says: "Error using vertcat. Dimensions of arrays being concatenated are not consistent". The word "vertcat" is a blue hyperlink.

输入参数

全部折叠

目标图窗,指定为 Figure 对象。图窗必须使用 uifigure 函数创建。

消息,指定为字符向量、字符向量元胞数组或字符串数组。消息可以为任意长度。

要显示多行文本,请使用字符向量元胞数组或字符串数组,其中数组中的每个元素均为不同文本行。

示例: 'Invalid value'

标题,指定为字符向量或字符串标量。长标题将被截断,后面用省略号表示。标题截断的确切长度取决于您的系统设置。

标题中的格式字符(例如换行符 \n)会替换为空格。

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: 'Icon','warning'

警报对话框关闭请求回调函数,指定为下列值之一:

  • 函数句柄。

  • 第一个元素是函数句柄的元胞数组。元胞数组中的后续元素是传递到回调函数的参量。

  • 作为有效 MATLAB 表达式的字符向量。MATLAB 在基础工作区中计算此表达式。

当 App 用户通过点击标题栏上的关闭按钮或点击确定按钮消除警告对话框时,将执行 CloseFcn 回调。当 App 用户从 MATLAB 命令行中关闭显示警告对话框的图窗窗口时,也会执行 CloseFcn 函数。您可以使用 close 函数从命令行中关闭图窗窗口。

图标,指定为预定义图标或自定义图标。

预定义图标

下表列出了预定义图标的值。例如,要显示对勾图标,请指定名称-值对组 'Icon','success'

图标
'error'(默认值)

Red octagon with an exclamation point symbol.

'warning'

Yellow triangle with an exclamation point symbol.

'info'

Blue square with the letter "i".

'question'

Blue circular icon with a question mark

'success'

Green circle with a check mark symbol.

''

不显示任何图标。

自定义图标

可将自定义图标指定为下列值之一:

  • 字符向量,指定 MATLAB 路径中的 SVG、JPEG、GIF 或 PNG 图像的文件名。也可以指定图像文件的完整路径。

  • 真彩色图像数组。有关详细信息,请参阅 图像类型

模态,指定为 truefalse

模态对话框有一个特点:若不关闭它,将无法访问图窗。但是,它不会阻止其他对话框出现。

对话框文本解释器,指定为:

  • 'none' - 显示字面字符。

  • 'tex' - 使用 TeX 标记的子集解释文本。

  • 'latex' - 使用 LaTeX 标记子集解释文本。

  • 'html' - 使用 HTML 标记子集解释文本。

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

©

LaTeX 标记

使用 LaTeX 标记来格式化和显示数学表达式、方程和特殊字符。使用美元符号将标记的文本括起来。例如,对于行内模式,使用 '$\int_1^{20} x^2 dx$',对于显示模式,使用 '$$\int_1^{20} x^2 dx$$'

显示的文本将使用默认的 LaTeX 字体样式。您可以使用 LaTeX 标记来更改字体样式。

MATLAB 支持大多数标准 LaTeX 数学模式命令。有关详细信息,请参阅支持的 LaTeX 命令

HTML 标记

使用 HTML 标记显示链接和自定义字体样式。

解释器支持部分 HTML 标记。一般情况下,解释器支持与文本相关的标记和样式。不支持的标记和样式将被忽略。

下表列出了支持的元素和元素属性。

HTML 元素属性描述
astyle, target, href, title超链接
abbrstyle, title缩写
addressstyle联系信息
articlestyle自包含的独立内容
asidestyle与主要内容间接相关的内容
bstyle粗体文本
bdistyle, dir格式与周围文本方向不同的内容
bdostyle, dir格式与周围文本方向不同的内容
bigstyle比周围文本大一个字体大小级别的文本(在 HTML5 中已弃用)
blockquotestyle, cite扩展引用
br不适用换行符
captionstyle表标题
centerstyle水平居中的内容
citestyle创造性工作的标题
codestyle代码片段
colstyle, align, valign, span, width表中的列
colgroupstyle, align, valign, span, width表中的一组列
ddstyle描述列表中的项或值
delstyle, datetime从文档中删除的文本
detailsstyle, open文本仅在切换到 'open' 状态时才可见的交互式小组件
dlstyle描述列表
dtstyle描述列表中的项或值
emstyle强调的文本(通常以粗体显示)
fontstyle, color, size, face具有指定字体属性的文本(在 HTML5 中弃用)
footerstyle页脚
h1. h2, h3, h4, h5, h6style节标题 - <h1> 是最高级别的标题,<h6> 是最低级别的标题
headerstyle介绍性内容
hrstyle主题换行
istyle偏离周围内容的文本 - 默认以粗体方式呈现
insstyle, datetime插入文档中的文本
listyle列表中的项
markstyle标记或突出显示的文本
olstyle有序列表
pstyle段落
prestyle预设格式的文本
sstyle带删除线的文本
strikestyle带删除线的文本(在 HTML5 中弃用)
sectionstyle独立节
smallstyle比周围文本小一个字体大小级别的文本(在 HTML5 中弃用)
substyle下标
supstyle上标
strongstyle非常重要的文本
tablestyle, width, border, align, valign
tbodystyle, align, valign表正文
tdstyle, width, rowspan, colspan, align, valign表数据单元格
tfootstyle, align, valign汇总表列的表行组
thstyle, width, rowspan, colspan, align, valign表数据单元格,指定为一组单元格的标题
theadstyle, align, valign指定列标题的表行组
trstyle, rowspan, align, valign表单元格行
ttstyle等宽文本(在 HTML5 中弃用)
ustyle具有模糊注解的文本 - 默认带下划线显示
ulstyle无序列表

有关这些元素的详细信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/HTML/Element

要使用 HTML 标记创建运行 MATLAB 代码的超链接,请参阅创建运行函数的超链接

您可以使用 HTML 样式属性来格式化 HTML 内容。样式属性是一串 CSS 属性及其值。

支持下列 CSS 属性:

  • background-color

  • border-bottom

  • border-bottom-color

  • border-bottom-left-radius

  • border-bottom-right-radius

  • border-bottom-style

  • border-bottom-width

  • border-left

  • border-left-color

  • border-left-style

  • border-left-width

  • border-radius

  • border-right

  • border-right-color

  • border-right-style

  • border-right-width

  • border-spacing

  • border-style

  • border-top

  • border-top-color

  • border-top-left-radius

  • border-top-right-radius

  • border-top-style

  • border-top-width

  • border-width

  • color

  • direction

  • font-family

  • font-size

  • font-style

  • font-weight

  • height

  • hidden

  • line-height

  • margin

  • margin-bottom

  • margin-left

  • margin-right

  • margin-top

  • max-height

  • max-width

  • min-height

  • min-width

  • overflow

  • overflow-wrap

  • overflow-x

  • overflow-y

  • padding

  • padding-bottom

  • padding-left

  • padding-right

  • padding-top

  • text-align

  • text-anchor

  • text-decoration

  • text-indent

  • text-overflow

  • text-shadow

  • text-transform

  • title

  • translate

  • white-space

  • width

有关这些属性的详细信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/CSS/Reference

版本历史记录

在 R2016a 中推出

全部展开