How can I suppress the warning caused by %#ok<NASGU> in MATLAB R2023b or later?

23 views (last 30 days)
I have used the %#ok<NASGU> directive extensively in my MATLAB code to suppress warnings for unused variables. However, in MATLAB R2023b or later this directive itself is generating a warning message: "A Code Analyzer message was once suppressed here, but the message is no longer generated." Given that my codebase is large, manually removing %#ok<NASGU> from multiple files is not feasible. Is there a way to suppress this new warning or an efficient method to remove the %#ok<NASGU> directive from all files?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Oct 2024
Here are two solutions to address this issue:
1. Disable the Warning in Your Code Base:
Please refer to the MATLAB documentation on configuring the Code Analyzer: https://www.mathworks.com/help/matlab/matlab_env/configure-code-analyzer.html
You can disable the specific warning related to %#ok<NASGU> by creating a "codeAnalyzerConfiguration.json" file. Here’s how you can do it:
Create a "codeAnalyzerConfiguration.json" file with the following content:
{ "checks" : { "MSNU": { "enabled": false } } }
Place this JSON file in the resources folder at the top level of your code base. This will disable the warning "MSNU: A Code Analyzer message was once suppressed here, but the message is no longer generated" for any sub-folders.
Note: Disabling this warning will also suppress other MSNU-related messages, not just those related to %#ok<NASGU>.
2. Use Code Analyzer Report to Remove %#ok<NASGU>:
The MATLAB Code Analyzer report can help you identify and remove %#ok<NASGU> directives from your code. Here’s how you can do it:
  • Run the Code Analyzer report on the folder containing your code.
  • The report will list all files with %#ok<NASGU> under the "Info" tab.
  • You can click "fix" to remove the %#ok<NASGU> directive from each file.
Although this method involves multiple clicks, it saves you from manually searching for each occurrence of the directive.
 
For large codebases, these solutions might not be perfect. An enhancement request has been created for developers to consider adding a "Fix All" button in the Code Analyzer report. This feature would allow the removal of all occurrences of %#ok<NASGU> with a single click.
  1 Comment
Walter Roberson
Walter Roberson on 30 Oct 2024
However, removing all of the <NASGU> would affect using the code in earlier releases, so this possibility is not ideal for multi-generational code usage.

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!