how to delete a custom warning identifier, not just "display off" ?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Cheng Li
am 9 Aug. 2023
Kommentiert: Adam Danz
am 10 Aug. 2023
I hope delete the custom warning identifier, but in Matlab, it seems like only hide it by setting state "off", however, the identifier is still there,how to actually remove the custom warning identifier
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 9 Aug. 2023
Bearbeitet: Adam Danz
am 9 Aug. 2023
> how to delete a custom warning identifier
If you want to permanently delete the custom warning ID so that it never appears anywhwere, find where the ID is defined and delete it from the file.
Suppressing a warning and removing it from lastwarn
When a specific warning is suppressed using warning(state,warnID), the warning is still picked up by lastwarn.
The syntax warning('') resets the warning state of lastwarn. Here's a demo that illustrate this behavior.
Throw a warning in MATLAB
A = eye(2);
B = [3 6; 4 8];
C = B\A;
lastwarn
Clear the lastwarn state
warning('')
lastwarn
Produce the warning again
C = B\A;
Now suppress this specific warning
warning('off','MATLAB:singularMatrix')
Show that the warning no longer appears
C = B\A; % no warning
Show that lastwarn still stores the warning
lastwarn
The problem of using warning('')
The problem with using warning('') is that it will reset the lastwarn state no matter what the last warning is. Let's say MATLAB issued an important warning just before the line of code that produces the warning you'd like to suppress. If you call warning('') MATLAB will indicate that no warning was thrown at all.
Revert lastwarn instead of clearing it
If you want to suppress a specific warning and prevent lastwarn from returning the specific warning, you can store the lastwarn state before the warning appears and return the state after the section that would throw the warning. I use onCleanup to return the warning state.
Example
warning('Example warning')
lastwarn
% store the current lastwarn state
[lastw.msg, lastw.id] = lastwarn;
% suppress the specific warning
warning('off','MATLAB:singularMatrix')
% Ensure that the original warn state is returned
% when lastwarnCleanup is cleared
lastwarnCleanup = onCleanup(@() lastwarn(lastw.msg, lastw.id));
% Here's the code that normally throws the warning we suppressed.
% No warning is shown.
C = B\A; % warning ID: MATLAB:singularMatrix
% This line is optional but useful in a script.
clear lastwarnCleanup
Now test lastwarn. Notice that it does not return MATLAB:singularMatrix warning.
lastwarn
Temporary warning suppression
Sometimes you may only want to suppress a warning locally within a function or script rather than for the entire MATLAB session. In those cases, you can use onCleanup to restore the initial warning state at the end of the function or when the cleanup object is deleted.
From the example above, instead of calling
warning('off','MATLAB:singularMatrix')
You can replace that line with this.
warnID = 'MATLAB:singularMatrix';
origState = warning('query', warnID);
cleanupWarningState = onCleanup(@()warning(origState));
warning('off', warnID)
which will revert to the original warning state after cleanupWarningState is cleared.
9 Kommentare
Weitere Antworten (1)
Walter Roberson
am 9 Aug. 2023
lastwarn
warning('IEH754H GETMAIN cannot provide buffer')
lastwarn
warning('')
lastwarn
Siehe auch
Kategorien
Mehr zu Error Handling finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!