主要内容

warning

R2026b

显示和管理警告

描述

显示警告

warning(msg) 显示警告消息 msg 并设置 lastwarn 函数返回的值。如果 msg 为空,warning("") 会重置 lastwarn 而不显示任何文本。

示例

warning(msg,A1,...,An) 显示一条警告消息,其中包含格式设置转换字符,例如随 MATLAB® sprintf 函数一起使用的字符。msg 中的每个转换字符都会转换为 A1,...,An 中对应的标量值。

warning(warnID,___) 将警告标识符附加至警告消息。您可以包括上述语法中的任何输入参量。您可以使用警告标识符区分警告并控制其状态(启用或禁用)。

管理警告

warning 显示所有警告的状态。

warning(state) 启用或禁用所有警告,或显示其状态。

示例

warning(state,warnID) 启用或禁用指定警告,或显示其状态。

warnStruct = warning 返回一个结构体数组,其中包含有关警告的当前状态的信息。warnStruct 中的每个元素包含一个具有 warnID 或 "all" 的 identifier 字段,以及一个指示警告是禁用还是启用的 state 字段。

示例

warning(warnStruct) 设置在 warnStruct 中指定的警告的状态。

控制显示

modeStruct = warning(state,mode) 通过将指定的 mode 设置为 state,控制 MATLAB 是显示栈跟踪还是附加信息。可选的返回结构体 modeStruct 包含有关上一个模式状态的信息。

示例

warning(modeStruct) 设置在 modeStruct 中指定的模式状态。

示例

全部折叠

生成带可选格式设定符和警告标识符的警告消息。

显示基本警告消息。

n = 7;
if ~ischar(n)
   warning("Input must be a character vector")
end
Warning: Input must be a character vector

使用格式设定符在警告消息中包含有关 n 的信息。

if ~ischar(n)
   warning("Input must be a character vector, not a %s",class(n))
end
Warning: Input must be a character vector, not a double

将警告标识符附加至警告消息。

if ~ischar(n)
   warning("MyComponent:incorrectType",...
       "Input must be a character vector, not a %s",class(n))
end
Warning: Input must be a character vector, not a double 

启用、禁用和查询所有或指定警告的警告状态。

禁用所有警告。

warning("off")

查询所有警告的状态。

warning
All warnings have the state "off".

启用所有警告并禁用特定奇异矩阵警告。然后查询所有警告的状态。

warning("on")
warning("off","MATLAB:singularMatrix")
warning
The default warning state is "on". Warnings not set to the default are

State  Warning Identifier

    off  MATLAB:singularMatrix

重新启用奇异矩阵警告。

warning("on","MATLAB:singularMatrix")

保存当前警告状态,修改它们,并还原保存的警告状态。

启用所有警告,然后禁用奇异矩阵。

warning("on")
warning("off","MATLAB:singularMatrix")

保存警告的当前状态。

s = warning
s = 

2x1 struct array with fields:

    identifier
    state

检查两个结构体。

s(1)
ans = 

    identifier: 'all'
         state: 'on'
s(2)
ans = 

    identifier: 'MATLAB:singularMatrix'
         state: 'off'

将启用除 "MATLAB:singularMatrix" 之外的所有警告。

通过禁用所有警告来修改警告。然后查询所有警告。

warning("off")
warning("query")
All warnings have the state 'off'.

还原保存的警告状态,并查询状态以进行验证。

warning(s)
warning("query")
The default warning state is 'on'. Warnings not set to the default are

State  Warning Identifier

    off  MATLAB:singularMatrix

控制 MATLAB 是否显示警告的栈跟踪或附加信息。

将 verbose 和 backtrace 设置为其默认状态。

warning("off","verbose")
warning("on","backtrace")

启用所有警告,并通过删除 MATLAB 路径中不存在的文件夹来显示警告。

warning("on")
rmpath("nosuchfolder")
Warning: "nosuchfolder" not found in path. 
> In rmpath at 57 

启用 "verbose" 以显示警告的附加信息。

warning("on","verbose")
rmpath("nosuchfolder")
Warning: "nosuchfolder" not found in path.
(Type "warning off MATLAB:rmpath:DirNotFound" to suppress this warning.)
 
> In rmpath at 57 

禁用栈跟踪显示。

warning("off","backtrace")
rmpath("nosuchfolder")
Warning: "nosuchfolder" not found in path.
(Type "warning off MATLAB:rmpath:DirNotFound" to suppress this warning.)

临时禁用特定警告,然后还原其原始状态。

通过计算奇异矩阵生成警告。

A = eye(2);
B = [3 6; 4 8];
C = B\A;
Warning: Matrix is singular to working precision.

查找此警告的警告标识符,保存其警告状态,并禁用此警告。

[msgStr,warnId] = lastwarn;
warnStruct = warning("off",warnId);
C = B\A;

还原此警告的先前警告状态。

warning(warnStruct);
C = B\A;
Warning: Matrix is singular to working precision.

输入参数

全部折叠

有关警告的信息,指定为字符向量或字符串标量。此文本显示为警告消息。要设置消息格式,请使用转义序列,例如 \t 或 \n。还可以使用 sprintf 函数支持的任何格式设定符,例如 %s 或 %d。通过 A1,...,An 输入参量指定转换设定符的值。% 字符被视为格式设定符。在 msg 中使用 %% 表示百分比字符。有关详细信息,请参阅格式化文本。

如果您想要 MATLAB 转换警告消息中的特殊字符(例如 \t、\n、%s 和 %d),必须为 warning 指定多个输入参量。

示例: "Input must be a character vector."

用来替换 msg 中的转换设定符的一个或多个值,每个值指定为字符向量、字符串标量或数值标量。

警告标识符,指定为字符向量、字符串标量、"all" 或 "last"。使用警告标识符可以确定警告的来源,或控制您的程序中选定的一小部分警告。

警告标识符包括一个或多个组件字段和一个助记键字段。各字段必须用冒号分隔。例如,具有组件字段 component 和助记键字段 mnemonic 的警告标识符指定为 "component:mnemonic"。组件字段和助记键字段都必须以字母开头。其余字符可以是字母数字字符(A–Z、a–z、0–9)和下划线。空白字符不能出现在 warnID 中的任何位置。有关创建标识符的信息,请参阅 MException。

当您使用 warning(state,warnID) 设置警告的 state 时,warnID 可具有 "all" 或 "last" 这两个值中的一个值。使用 "all" 可设置所有警告的状态,使用 "last" 可设置最近发布的警告的状态。

示例: "MATLAB:singularMatrix"

示例: "MATLAB:narginchk:notEnoughInputs"

状态控制指示符,指定为 "on"、"off" 或 "query"。使用 "on" 或 "off" 启用或禁用警告,或启用或禁用警告显示 mode。使用 "query" 可查询警告或模式设置的当前状态。

警告状态,指定为结构体数组,其中包含有关哪些警告已启用和哪些已禁用的信息。warnStruct 包含一个具有 warnID 或 "all" 值的 identifier 字段,以及一个具有 "on" 或 "off" 值以指示对应警告状态的 state 字段。

警告显示模式,指定为 "backtrace" 或 "verbose"。使用 "backtrace" 控制是否显示警告的栈跟踪,使用 "verbose" 控制是否显示附加警告信息。默认情况下,"backtrace" 设置为 "on","verbose" 设置为 "off"。

输出参量

全部折叠

警告状态,指定为结构体数组,其中包含有关哪些警告已启用和哪些已禁用的信息。warnStruct 包含一个具有 warnID 或 "all" 值的 identifier 字段,以及一个具有 "on" 或 "off" 值以指示对应警告状态的 state 字段。

模式状态,指定为标量结构体,其中包含来自先前模式状态的信息。warnStruct 包含一个具有 "verbose" 或 "backtrace" 值的 identifier 字段,以及一个具有 "on" 或 "off" 值以指示对应模式状态的 state 字段。

扩展功能

全部展开

版本历史记录

在 R2006a 之前推出