what does the string #ok<NASGU> mean in the code editior

109 vues (au cours des 30 derniers jours)
Stephen Forczyk
Stephen Forczyk le 4 Oct 2019
Commenté : iBob le 25 Nov 2020
I have a m file that is sprinkled with lines such as...
some code % #ok<NASGU> what does this mean?

Réponse acceptée

Daniel M
Daniel M le 4 Oct 2019
Modifié(e) : Daniel M le 4 Oct 2019
Matlab's code analyzer, mlint, includes a wide range of warning messages about possible problems or inefficiencies in the analyzed m-file.
NASGU indicates the value assigned to variable name might be unused. Putting %#ok<NASGU> in a line of code tells mlint not to worry about this particular issue, on this line. You can see all the warnings as orange little tick marks near the scroll bar of the m-file. Red lines indicate syntax and other errors.
See this page for more information: Check for warnings and errors

Plus de réponses (3)

Steven Lord
Steven Lord le 4 Oct 2019
The %#ok pragma indicates that a particular Code Analyzer message has been suppressed on that line of code. See the "Adjust Code Analyzer Message Indicators and Messages" section on the documentation page to which I linked in my first sentence for more information.
You can search in the Code Analyzer preferences to identify which message has identifier NASGU and understand the potential issue Code Analyzer has detected on that line.

Kelly Kearney
Kelly Kearney le 4 Oct 2019
That code disables the Code Analyzer from suggesting changes to the syntax. Specifically, <NASGU> refers the the ID associated with the "The value assigned here to 'x' appears to be unused. Consider replacing it by ~" suggestion.
My guess is that code was written with back-compatibility in mind, so the author intentionally chose not to use the ~ syntax on a bunch of lines, and wanted to silence all the warnings related to that choice so they didn't distract from any actually useful syntax warnings.
For example, if you create the following function in a recent version of Matlab:
function x = testmlint(x)
[a,b] = max(x);
x = b*2;
you'll see the little orange bar off to the side of the [a,b] = line. This version:
function x = testmlint(x)
[a,b] = max(x); %#ok
x = b*2;
is orange-bar free.
  1 commentaire
Rik
Rik le 4 Oct 2019
When writing code yourself I would discourage the use of %#ok without the warning specifier. If you change your code that may cause a different warning. If you aren't using the specifier you are denying yourself the help of mlint.

Connectez-vous pour commenter.


iBob
iBob le 25 Nov 2020
> %#ok<NASGU>
I just discovered this feature and really like it. Why doesn't the Matlab comment mention that it is suppressing the Code Analyzer? I have an instance of
%#ok<NASGU> %unusable formula
for a line of code used as a flag. I can imagine an unaware person seeing the message and commenting or deleting the line of code.
  2 commentaires
Rik
Rik le 25 Nov 2020
I would say that is due to the comment, not the ok pragma itself. What would you suggest?
This specific example sounds like a badly written comment where the code might then be edited by someone who doesn't understand the code. Both sound like independent problems to me, and neither is really directly related to the pragma.
iBob
iBob le 25 Nov 2020
Rik, I fear you are correct that the comment was there before %#ok<NASGU> was added. My mistake, my apology.
> What would you suggest?
When I first spotted %#ok<NASGU>, I had no idea what it meant or where it came from. Something like
%#ok<NASGU> %Code Analyzer suppression
would be more informative. I'm a relative newcomer to Matlab and still on the learning curve. Thanks for helping me learn.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by