how to delete a custom warning identifier, not just "display off" ?

7 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Adam Danz
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;
Warning: Matrix is singular to working precision.
lastwarn
ans = 'Matrix is singular to working precision.'
Clear the lastwarn state
warning('')
lastwarn
ans = 0×0 empty char array
Produce the warning again
C = B\A;
Warning: Matrix is singular to working precision.
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
ans = 'Matrix is singular to working precision.'
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')
Warning: Example warning
lastwarn
ans = 'Example warning'
% 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
ans = 'Example warning'
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
Adam Danz
Adam Danz am 10 Aug. 2023
Thanks for following up with your explanation, @Cheng Li!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 9 Aug. 2023
lastwarn
ans = 0×0 empty char array
warning('IEH754H GETMAIN cannot provide buffer')
Warning: IEH754H GETMAIN cannot provide buffer
lastwarn
ans = 'IEH754H GETMAIN cannot provide buffer'
warning('')
lastwarn
ans = 0×0 empty char array
  1 Kommentar
Cheng Li
Cheng Li am 9 Aug. 2023
Thank you, however, maybe you mistake what I mean. I hope to delete my defining warning identifier not only the warning message. warning identifier is like this pattern of "mytool:mytypes" as the bult-in warning identifier "MATLAB:singularMatrix" which comes out when the matrix A is close to be singular in equations Ax=b,but whose warning message is string "Matrix is singular to working precision." and is what the function 'lastwarn' returns. The warning identifier can be created by giving 2 arguments to warning(warnID,warnMsg),if the first argument is in format of "string:string", it will be regarded as the warning identifier, and the second as the warning message. And the custom warning identifier is so difficult to remove

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Error Handling finden Sie in Help Center und File Exchange

Produkte


Version

R2014b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by