On this page
12.2.1 Issuing Warnings
It is possible to issue warnings from any code using the warning function. In its most simple form, the warning function takes a string describing the warning as its input argument. As an example, the following code controls if the variable ‘a’ is non-negative, and if not issues a warning and sets ‘a’ to zero.
a = -1;
if (a < 0)
warning ("'a' must be non-negative. Setting 'a' to zero.");
a = 0;
endif
-| 'a' must be non-negative. Setting 'a' to zero.
Since warnings aren’t fatal to a running program, it is not possible to catch a warning using the try statement or something similar. It is however possible to access the last warning as a string using the lastwarn function.
It is also possible to assign an identification string to a warning. If a warning has such an ID the user can enable and disable this warning as will be described in the next section. To assign an ID to a warning, simply call warning with two string arguments, where the first is the identification string, and the second is the actual warning. Note that warning IDs are in the format "NAMESPACE:WARNING-NAME". The namespace "Octave" is used for Octave’s own warnings. Any other string is available as a namespace for user’s own warnings.
- : warning (template, …) ¶
- : warning (id, template, …) ¶
- : warning ("on", id) ¶
- : warning ("off", id) ¶
- : warning ("error", id) ¶
- : warning ("query", id) ¶
- : warning (state, id, "local") ¶
- : warning (warning_struct) ¶
- : warning_struct = warning (…) ¶
- : warning (state, mode) ¶
-
Display a warning message or control the behavior of Octave’s warning system.
The first call form uses a template template and optional additional arguments to display a message on the
stderrstream. The message is formatted using the same rules as theprintffamily of functions (see Formatted Output) and prefixed by the character string ‘warning: ’. You should use this function when you want to notify the user of an unusual condition, but only when it makes sense for your program to go on. For example:warning ("foo: maybe something wrong here");If the warning message does not end with a newline character, Octave will print a traceback of all the function calls leading to the warning. If the warning message does end in a newline character, Octave will suppress the traceback messages as it returns control to the top level. For more details and examples, see
error.The optional warning identifier id allows users to enable or disable warnings tagged by this identifier. A message identifier is a string of the form
"NAMESPACE:WARNING-NAME". Octave’s own warnings use the"Octave"namespace (seewarning_ids). For example:warning ("MyNameSpace:check-something", "foo: maybe something wrong here");The second call form is meant to change and/or query the state of warnings. The first input argument must be a string state (
"on","off","error", or"query") followed by an optional warning identifier id or"all"(default).The optional output argument warning_struct is a structure or structure array with fields
"state"and"identifier". The state argument may have the following values:-
"on"|"off": -
Enable or disable the display of warnings identified by id and optionally return their previous state stout.
-
"error": -
Turn warnings identified by id into errors and optionally return their previous state stout.
-
"query": -
Return the current state of warnings identified by id.
A structure or structure array warning_struct, with fields
"state"and"identifier", may be given as an input to achieve equivalent results. The following example shows how to temporarily disable a warning and then restore its original state:loglog (-1:10); ## Disable the previous warning and save its original state [~, id] = lastwarn (); warnstate = warning ("off", id); loglog (-1:10); ## Restore its original state warning (warnstate);If a final argument
"local"is provided then the warning state will be set temporarily until the end of the current function. Changes to warning states that are set locally affect the current function and all functions called from the current scope. The previous warning state is restored on return from the current function. The"local"option is ignored if used in the top-level workspace.With no input argument
warning ()is equivalent towarning ("query", "all")except that in the absence of an output argument, the state of warnings is displayed onstderr.The level of verbosity of the warning system may also be controlled by two modes mode:
-
"backtrace": -
enable/disable the display of the stack trace after the warning message
-
"verbose": -
enable/disable the display of additional information after the warning message
In this case the state argument may only be
"on"or"off".Implementation Note: For compatibility with MATLAB, escape sequences in template (e.g.,
"\n"=> newline) are processed regardless of whether template has been defined with single quotes, as long as there are two or more input arguments. To disable escape sequence expansion use a second backslash before the sequence (e.g.,"\\n") or use theregexptranslatefunction.See also: warning_ids, lastwarn, error.
-
- : [msg, msgid] = lastwarn () ¶
- : lastwarn (msg) ¶
- : lastwarn (msg, msgid) ¶
-
Query or set the last warning message.
When called without input arguments, return the last warning message and message identifier.
With one argument, set the last warning message to msg.
With two arguments, also set the last message identifier to msgid.
The functions distributed with Octave can issue one of the following warnings.
-
Octave:abbreviated-property-match -
If the
Octave:abbreviated-property-matchwarning is enabled, a warning is printed if a non-exact or ambiguous match is being used for a operation specifying an object property. For example, for a graphics object, fig, with the property'displayname',get (fig, 'dis')elicits a warning if theOctave:abbreviated-property-matchwarning is enabled. By default, theOctave:abbreviated-property-matchwarning is enabled. -
Octave:addpath-pkg -
If the
Octave:addpath-pkgwarning is enabled, Octave will warn when a package directory (i.e., +package_name) is added to thepath. Typically, only the parent directory which contains the package directory should be added to the load path. By default, theOctave:addpath-pkgwarning is enabled. -
Octave:array-as-logical -
If the
Octave:array-as-logicalwarning is enabled, Octave will warn when an array of size greater than 1x1 is used as a truth value in an if, while, or until statement. By default, theOctave:array-as-logicalwarning is disabled. -
Octave:array-to-scalar -
If the
Octave:array-to-scalarwarning is enabled, Octave will warn when an implicit conversion from an array to a scalar value is attempted. By default, theOctave:array-to-scalarwarning is disabled. -
Octave:array-to-vector -
If the
Octave:array-to-vectorwarning is enabled, Octave will warn when an implicit conversion from an array to a vector value is attempted. By default, theOctave:array-to-vectorwarning is disabled. -
Octave:assign-as-truth-value -
If the
Octave:assign-as-truth-valuewarning is enabled, a warning is issued for statements likeif (s = t) …since such statements are not common, and it is likely that the intent was to write
if (s == t) …instead.
There are times when it is useful to write code that contains assignments within the condition of a
whileorifstatement. For example, statements likewhile (c = getc ()) …are common in C programming.
It is possible to avoid all warnings about such statements by disabling the
Octave:assign-as-truth-valuewarning, but that may also let real errors likeif (x = 1) # intended to test (x == 1)! …slip by.
In such cases, it is possible suppress errors for specific statements by writing them with an extra set of parentheses. For example, writing the previous example as
while ((c = getc ())) …will prevent the warning from being printed for this statement, while allowing Octave to warn about other assignments used in conditional contexts.
By default, the
Octave:assign-as-truth-valuewarning is enabled. -
Octave:autoload-relative-file-name -
If the
Octave:autoload-relative-file-nameis enabled, Octave will warn when parsing autoload() function calls with relative paths to function files. This usually happens when using autoload() calls in PKG_ADD files, when the PKG_ADD file is not in the same directory as the .oct file referred to by the autoload() command. By default, theOctave:autoload-relative-file-namewarning is enabled. -
Octave:charmat-truncated -
If the
Octave:charmat-truncatedwarning is enabled, a warning is printed when a character matrix with multiple rows is converted to a string. In this case, the Octave interpreter keeps only the first row and discards the others. By default, theOctave:charmat-truncatedwarning is enabled. -
Octave:classdef-to-struct -
If the
Octave:classdef-to-structwarning is enabled, a warning is issued when a classdef object is forcibly converted into a struct withstruct (CLASSDEF_OBJ). Conversion removes the access restrictions from the object and makes private and protected properties visible. By default, theOctave:classdef-to-structwarning is enabled. -
Octave:colon-complex-argument -
If the
Octave:colon-complex-argumentwarning is enabled, a warning is issued when one of the three arguments to the colon operator (base, increment, limit) is a complex value. For example,1:3*iwill cause a warning to be emitted. By default, theOctave:colon-complex-argumentwarning is enabled. -
Octave:colon-nonscalar-argument -
If the
Octave:colon-nonscalar-argumentwarning is enabled, a warning is issued when one of the three arguments to the colon operator (base, increment, limit) is not a scalar. For example,1:[3, 5]will cause a warning to be emitted. By default, theOctave:colon-nonscalar-argumentwarning is enabled. -
Octave:data-file-in-path -
If the
Octave:data-file-in-pathwarning is enabled, a warning is issued when Octave does not find the target of a file operation such asloadorfopendirectly, but is able to locate the file in Octave’s searchpathfor files. The warning could indicate that a different file target than the programmer intended is being used. By default, theOctave:data-file-in-pathwarning is enabled. -
Octave:datevec:date-format-spec -
If the
Octave:datevec:date-format-specwarning is enabled, a warning is printed if the date format specification contains questionable date or time specifiers. Typical bad patterns are using uppercase date specifiers or lowercase time specifiers. By default, theOctave:datevec:date-format-specwarning is enabled. -
Octave:deprecated-function -
If the
Octave:deprecated-functionwarning is enabled, a warning is issued when Octave encounters a function that is obsolete and scheduled for removal from Octave. By default, theOctave:deprecated-functionwarning is enabled. -
Octave:deprecated-keyword -
If the
Octave:deprecated-keywordwarning is enabled, a warning is issued when Octave encounters a keyword that is obsolete and scheduled for removal from Octave. By default, theOctave:deprecated-keywordwarning is enabled. -
Octave:deprecated-option -
If the
Octave:deprecated-optionwarning is enabled, a warning is issued when an obsolete option or input to a function is used. By default, theOctave:deprecated-optionwarning is enabled. -
Octave:deprecated-property -
If the
Octave:deprecated-propertywarning is enabled, a warning is issued when Octave encounters a graphics property that is obsolete and scheduled for removal from Octave. By default, theOctave:deprecated-propertywarning is enabled. -
Octave:eigs:UnconvergedEigenvalues -
If the
Octave:eigs:UnconvergedEigenvalueswarning is enabled then the eigs function will issue a warning if the number of calculated eigenvalues is less than the number of requested eigenvalues. By default, theOctave:eigs:UnconvergedEigenvalueswarning is enabled. -
Octave:empty-index -
If the
Octave:empty-indexwarning is enabled then Octave will emit a warning whenever indexing operators are used without an index, for examplex(). By default, theOctave:empty-indexwarning is enabled. -
Octave:erase:chararray -
If the
Octave:erase:chararraywarning is enabled then the erase function will issue a warning if the input pattern is a character array rather than a string or cell array of strings. By default, theOctave:erase:chararraywarning is enabled. -
Octave:function-name-clash -
If the
Octave:function-name-clashwarning is enabled, a warning is issued when Octave finds that the name of a function defined in a function file differs from the name of the file. (If the names disagree, the name declared inside the file is ignored.) By default, theOctave:function-name-clashwarning is enabled. -
Octave:future-time-stamp -
If the
Octave:future-time-stampwarning is enabled, Octave will print a warning if it finds a function file with a time stamp that is in the future. By default, theOctave:future-time-stampwarning is enabled. -
Octave:glyph-render -
If the
Octave:glyph-renderwarning is enabled, Octave will print a warning if the glyph for a character couldn’t be rendered with the current font. By default, theOctave:glyph-renderwarning is enabled. -
Octave:imag-to-real -
If the
Octave:imag-to-realwarning is enabled, a warning is printed for implicit conversions of complex numbers to real numbers. By default, theOctave:imag-to-realwarning is disabled. -
Octave:infinite-loop -
If the
Octave:infinite-loopwarning is enabled, a warning is printed when an infinite loop is detected such asfor i = 1:Inforwhile (1). By default, theOctave:infinite-loopwarning is enabled. -
Octave:language-extension -
Print warnings when using features that are unique to the Octave language and that may still be missing in MATLAB. By default, the
Octave:language-extensionwarning is disabled. The --traditional or --braindead startup options for Octave may also be of use, see Command Line Options. -
Octave:legacy-function -
If the
Octave:legacy-functionwarning is enabled, a warning is issued when Octave encounters a function that MATLAB has suggested should be avoided. The function may become obsolete at some point in the future and removed, in which case the warning will change toOctave:deprecated-function, and the function will continue to exist for two further versions of Octave before being removed. By default, theOctave:legacy-functionwarning is enabled. -
Octave:logical-conversion -
If the
Octave:logical-conversionwarning is enabled, a warning is printed if an implicit conversion of an array from numerical to boolean occurs and any of the elements in the array are not equal to zero or one. By default, theOctave:logical-conversionwarning is enabled. -
Octave:lu:sparse_input -
If the
Octave:lu:sparse_inputwarning is enabled, Octave will warn when the lu function is called with a sparse input and less than four output arguments. In this case, sparsity-preserving column permutations are not performed and the result may be inaccurate. By default, theOctave:lu:sparse_inputwarning is enabled. -
Octave:missing-glyph -
If the
Octave:glyph-renderwarning is enabled, Octave will print a warning if the current font doesn’t provide a glyph for a used character. By default, theOctave:missing-glyphwarning is enabled. -
Octave:missing-semicolon -
If the
Octave:missing-semicolonwarning is enabled, Octave will warn when statements in function definitions don’t end in semicolons. By default theOctave:missing-semicolonwarning is disabled. -
Octave:mixed-string-concat -
If the
Octave:mixed-string-concatwarning is enabled, print a warning when concatenating a mixture of double and single quoted strings. By default, theOctave:mixed-string-concatwarning is disabled. -
Octave:nearly-singular-matrix -
Octave:singular-matrix -
These warnings are emitted if a (nearly) singular matrix is inverted. By default, the
Octave:nearly-singular-matrixandOctave:singular-matrixwarnings are enabled. -
Octave:neg-dim-as-zero -
If the
Octave:neg-dim-as-zerowarning is enabled, print a warning for expressions likeeye (-1)By default, the
Octave:neg-dim-as-zerowarning is disabled. -
Octave:noninteger-range-as-index -
If the
Octave:noninteger-range-as-indexwarning is enabled, a warning is printed if an array is indexed with a range that contains non-integer values. For example,a = [1 2 3 4 5]; b = 2.2:4.2 ⇒ 1.2 2.2 3.2 a(b) ⇒ 2 3 4elicits a warning if the
Octave:noninteger-range-as-indexwarning is enabled. By default, theOctave:noninteger-range-as-indexwarning is enabled. -
Octave:num-to-str -
If the
Octave:num-to-strwarning is enabled, a warning is printed for implicit conversions of numbers to their UTF-8 encoded character equivalents when strings are constructed using a mixture of strings and numbers in matrix notation. For example,[ "f", 111, 111 ] ⇒ "foo"elicits a warning if the
Octave:num-to-strwarning is enabled. By default, theOctave:num-to-strwarning is enabled. -
Octave:possible-matlab-short-circuit-operator -
If the
Octave:possible-matlab-short-circuit-operatorwarning is enabled, Octave will warn about using the not short circuiting operators&and|insideiforwhileconditions. They normally never short circuit, but they do short circuit when used in a condition. By default, theOctave:possible-matlab-short-circuit-operatorwarning is enabled. -
Octave:pow2:imaginary-ignored -
If the
Octave:pow2:imaginary-ignoredwarning is enabled, a warning is printed if either input topow2is complex. By default, theOctave:pow2:imaginary-ignoredwarning is enabled. -
Octave:recursive-path-search -
If the
Octave:recursive-path-searchwarning is enabled, Octave will issue a warning ifaddpathis used with double trailing slashes. By default, theOctave:recursive-path-searchwarning is enabled. -
Octave:remove-init-dir -
The
pathfunction changes the search path that Octave uses to find functions. It is possible to set the path to a value which excludes Octave’s own built-in functions. If theOctave:remove-init-dirwarning is enabled then Octave will warn when thepathfunction has been used in a way that may render Octave unworkable. By default, theOctave:remove-init-dirwarning is enabled. -
Octave:reload-forces-clear -
If several functions have been loaded from the same file, Octave must clear all the functions before any one of them can be reloaded. If the
Octave:reload-forces-clearwarning is enabled, Octave will warn you when this happens, and print a list of the additional functions that it is forced to clear. By default, theOctave:reload-forces-clearwarning is enabled. -
Octave:separator-insert -
Print warning if commas or semicolons might be inserted automatically in literal matrices. By default, the
Octave:separator-insertwarning is disabled. -
Octave:shadowed-function -
If the
Octave:shadowed-functionwarning is enabled, Octave will warn if a path is added to the search path that contains functions that shadow core functions. By default, theOctave:shadowed-functionwarning is enabled. -
Octave:single-quote-string -
Print warning if a single quote character is used to introduce a string constant. By default, the
Octave:single-quote-stringwarning is disabled. -
Octave:sqrtm:SingularMatrix -
If the
Octave:sqrtm:SingularMatrixwarning is enabled, a warning is printed if the matrix square root functionsqrtmis called with an input matrix that is singular. By default, theOctave:sqrtm:SingularMatrixwarning is enabled. -
Octave:str-to-num -
If the
Octave:str-to-numwarning is enabled, a warning is printed for implicit conversions of strings to their numeric UTF-8 encoded byte sequences. For example,"abc" + 0 ⇒ 97 98 99elicits a warning if the
Octave:str-to-numwarning is enabled. By default, theOctave:str-to-numwarning is disabled. -
Octave:LaTeX:internal-error -
If the
Octave:LaTeX:internal-errorwarning is enabled, a warning is printed whenever the LaTeX renderer for text in plots encounters an issue. By default, theOctave:LaTeX:internal-errorwarning is enabled. -
Octave:unimplemented-matlab-functionality -
If the
Octave:unimplemented-matlab-functionalitywarning is enabled, a warning is printed when a MATLAB code construct is used which the Octave interpreter parses as valid, but for which Octave does not yet implement the functionality. By default, theOctave:unimplemented-matlab-functionalitywarning is enabled. -
Octave:variable-switch-label -
If the
Octave:variable-switch-labelwarning is enabled, Octave will print a warning if a switch label is not a constant or constant expression. By default, theOctave:variable-switch-labelwarning is disabled.
© 1996–2023 The Octave Project Developers
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://docs.octave.org/v8.1.0/Issuing-Warnings.html