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 identifierMATLAB:warning.warning(id, fmt, args...)associates the warning with the identifierid, formats the message using MATLAB'ssprintfrules, and honours per-identifier state.warning(MException_obj)reissues an existing exception as a warning.warning(struct)restores warning state previously captured withwarning('query', ...).warning(state, id)changes the mode forid('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)resetsid(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 forid. 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
warninglater 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
- 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
lastwarnbuiltin. - 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. - How do I restore defaults after experimentation? Call
warning('reset')to revert the global default, per-identifier table, diagnostic modes, and once-tracking. - Do state-changing calls return anything useful? Yes. Forms such as
warning('off','id'),warning('on','backtrace'), andwarning('default')return struct snapshots describing the state before the change. Store the struct and pass it back towarninglater to restore the captured configuration. - What does the
'verbose'mode do? When enabled (warning('on','verbose')), RunMat prints an additional hint describing how to suppress the warning. Disable it withwarning('default','verbose'). - 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. - Does
warningrun 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. - Can I provide multiple state structs to
warning(state)? Yes. Pass either a struct or a cell array of structs (as returned bywarning('query','all')); RunMat applies them sequentially. - 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. - 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:. - What if I call
warningwith unsupported arguments? RunMat issues a MATLAB-compatible usage error so you can correct the call. - Are repeated calls thread-safe? Internally the warning table is guarded by a mutex, so concurrent invocations are serialised and remain deterministic.
See Also
Source & Feedback
- The full source code for the implementation of the
warningfunction is available at:crates/runmat-runtime/src/builtins/diagnostics/warning.rs - Found a bug or behavioural difference? Please open an issue with details and a minimal repro.