Main Content

warning

显示警告消息

说明

示例

warning(msg) 显示警告消息并设置 lastwarn 函数的警告状态。如果 msg 为空,则 warning 将重置 lastwarn 的警告状态,但不显示任何文本。

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

warning(warnID,___) 将警告标识符附加至警告消息。您可以包括上述语法中的任何输入参数。此标识符可用于区分警告并控制在 MATLAB 遇到警告时发生的情况。

示例

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

warning(state,warnID) 处理指定警告的状态。

warning 显示所有警告的状态。它等效于 warning('query')

示例

warnStruct = warning 返回一个结构体或一个包含有关启用和禁用哪些警告的信息的结构体数组。warnStruct 包括一个 identifier 字段(其中包含 warnID'all'),以及一个指示相应警告状态的 state 字段。

warning(warnStruct) 按照结构体数组 warnStruct 中的说明设置当前警告设置。

示例

warning(state,mode) 控制 MATLAB 是否显示堆栈跟踪或有关警告的其他信息。

warnStruct = warning(state,mode) 返回一个结构体,其中有一个包含 modeidentifier 字段和一个包含 mode 当前状态的 state 字段。如果将输出结构体 warnStruct 传递到 warning 函数中,您可设置 mode 的状态,而非启用或禁用哪些警告。

示例

全部折叠

生成一个显示消息的警告。

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

确保 verbosebacktrace 设置是默认值。

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

打开所有警告,并移除 MATLAB 路径上不存在的文件夹。

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

启用详细程度以显示展开的警告消息。

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。通过 A 输入参数指定转换设定符的值。有关详细信息,请参阅格式化文本

注意

如果您想要 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

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

示例: 'MATLAB:singularMatrix'

示例: 'MATLAB:narginchk:notEnoughInputs'

警告控制指示符,指定为 'on''off''query'。使用 'on''off' 可控制 MATLAB 是否发出警告。使用 'query' 可查询警告的当前状态。

警告设置,指定为包含有关启用和禁用哪些警告的信息的结构体或结构体数组。warnStruct 包括一个 identifier 字段(带有 warnID'all')以及指示相应警告状态的 state 字段。

详细程度和堆栈跟踪显示设置,由 'backtrace''verbose' 指定。默认情况下,详细程度状态设置为 'off',堆栈跟踪显示的状态设置为 'on'

扩展功能

版本历史记录

在 R2006a 之前推出