View all functions

CategoryDiagnostics

What does the warning function do in MATLAB / RunMat?

warning emits diagnostic messages without aborting execution. It also provides a central interface for enabling, disabling, promoting, or querying warnings by identifier.

RunMat mirrors MATLAB semantics with per-identifier modes ('on', 'off', 'once', 'error'), backtrace control, structured state restoration, and formatted message support. When an identifier is promoted to 'error', future uses of that warning raise an error instead of printing.

How does the warning function behave in MATLAB / RunMat?

  • warning(message) prints the message using the default identifier MATLAB:warning.
  • warning(id, fmt, args...) associates the warning with the identifier id, formats the message using MATLAB's sprintf rules, and honours per-identifier state.
  • warning(MException_obj) reissues an existing exception as a warning.
  • warning(struct) restores warning state previously captured with warning('query', ...).
  • warning(state, id) changes the mode for id ('on', 'off', 'once', 'error', and the aliases 'all' or 'last'). The call returns a struct describing the prior state so you can restore it later.
  • warning(state, mode) controls diagnostic verbosity modes such as 'backtrace' and 'verbose', returning a struct snapshot of the previous setting.
  • warning('default', id) resets id (or 'all', 'backtrace', 'verbose', 'last') to the factory default, returning the state that was active before the reset. warning('reset') restores every warning, mode, and once-tracking flag in one call.
  • warning('query', id) returns a struct describing the current mode for id. Passing 'all' returns a cell vector of structs covering every configured identifier plus the global default and diagnostic modes.
  • warning('status') prints the global warning table as a formatted summary.
  • All state-changing forms return struct snapshots; pass them back to warning later to reinstate the captured state.

GPU execution and residency

warning is a control-flow builtin that runs on the host. When formatted arguments include GPU resident arrays (for example via %g specifiers), RunMat gathers the values to host memory before formatting the message so the diagnostic text matches MATLAB expectations.

Examples of using the warning function in MATLAB / RunMat

Displaying a simple warning

warning("Computation took longer than expected.");

Emitting a warning with a custom identifier

warning("runmat:io:deprecatedOption", ...
        "Option '%s' is deprecated and will be removed in %s.", ...
        "VerboseMode", "1.2");

Turning a specific warning off and back on

warning("off", "runmat:io:deprecatedOption");
% ... code that triggers the warning ...
warning("on", "runmat:io:deprecatedOption");

Promoting a warning to an error

warning("error", "runmat:solver:illConditioned");
% The next call raises an error instead of printing a warning.
warning("runmat:solver:illConditioned", ...
        "Condition number exceeds threshold %.2e.", 1e12);

Querying and restoring warning state

state = warning("query", "all");   % capture the current table
warning("off", "all");              % silence all warnings temporarily
warning("Some temporary warning.");
warning(state);                    % restore original configuration

Enabling backtraces for debugging

warning("on", "backtrace");   % warning("backtrace","on") is equivalent
warning("runmat:demo:slowPath", "Inspect the call stack above for context.");
warning("off", "backtrace");

Displaying verbose suppression guidance

warning("on", "verbose");
warning("runmat:demo:slowPath", "Inspect the call stack above for context.");
warning("default", "verbose");   % restore the default verbosity

FAQ

  1. Does RunMat keep track of the last warning? Yes. The last identifier and message are stored internally and will be exposed through the MATLAB-compatible lastwarn builtin.
  2. What does 'once' do? The first occurrence of the warning is shown, and subsequent uses of the same identifier are suppressed until you change the state.
  3. How do I restore defaults after experimentation? Call warning('reset') to revert the global default, per-identifier table, diagnostic modes, and once-tracking.
  4. Do state-changing calls return anything useful? Yes. Forms such as warning('off','id'), warning('on','backtrace'), and warning('default') return struct snapshots describing the state before the change. Store the struct and pass it back to warning later to restore the captured configuration.
  5. What does the 'verbose' mode do? When enabled (warning('on','verbose')), RunMat prints an additional hint describing how to suppress the warning. Disable it with warning('default','verbose').
  6. What happens when a warning is promoted to 'error'? The warning text is reused to raise an error with the same identifier, just like MATLAB.
  7. Does warning run on the GPU? No. Control-flow builtins execute on the host. GPU inputs referenced in formatted messages are gathered automatically before the message is emitted.
  8. Can I provide multiple state structs to warning(state)? Yes. Pass either a struct or a cell array of structs (as returned by warning('query','all')); RunMat applies them sequentially.
  9. Does warning('on','backtrace') match MATLAB output exactly? RunMat prints a Rust backtrace for now. Future releases will map this to MATLAB-style stack frames, but the enable/disable semantics match MATLAB.
  10. Is whitespace in identifiers allowed? Identifiers follow MATLAB rules: they must contain at least one colon and no whitespace. RunMat normalises bare names by prefixing MATLAB:.
  11. What if I call warning with unsupported arguments? RunMat issues a MATLAB-compatible usage error so you can correct the call.
  12. Are repeated calls thread-safe? Internally the warning table is guarded by a mutex, so concurrent invocations are serialised and remain deterministic.

See Also

error, sprintf, fprintf

Source & Feedback