필터 지우기
필터 지우기

MATLAB Editor Code Analyzer

조회 수: 1 (최근 30일)
Alan
Alan 2012년 4월 3일
For the following code
x=1;
save('x','x')
x=2;
the MATLAB Editor Code Analyzer warns
"The value assigned to variable 'x' might be unused." I have clearly used it; I saved it to the file 'x.mat'. This bugs me, I'd like to see a nice green box telling me my code is clean.

채택된 답변

Rick Rosson
Rick Rosson 2012년 4월 3일
Add the %#ok pragma at the end of the line:
x = 2; %#ok
HTH.
  댓글 수: 1
Alan
Alan 2012년 4월 3일
Thanks, I found it at the same time. Actually, the comment should go on the line
x=1;

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Honglei Chen
Honglei Chen 2012년 4월 3일
The code analyzer cannot link 'x' to x because the former may just be a character and has nothing to do with the variable. You can get around this by right clicking the warning and suppressing the warning from the editor.
  댓글 수: 2
Alan
Alan 2012년 4월 3일
That doesn't seem to work on my system. I am using OS X.
Alan
Alan 2012년 4월 3일
but terminating my line with %#ok does work. Thanks for the lead.

댓글을 달려면 로그인하십시오.


Rick Rosson
Rick Rosson 2012년 4월 3일
Technically speaking (perhaps hyper-technically), you have not used the variable x. What you have done is to call a function and pass into that function two literal string constants. You have not passed any variables into the function.
So, the MATLAB Code Analyzer is actually correct. That being said, you are rightly disappointed by this outcome in this case. The reality is that the Code Analyzer does a really good job helping programmers write better code, but it is not perfect in all cases. MATLAB is an extremely flexible and forgiving language that allows programmers to do amazing things. As a consequence, it is not possible for any static code analyzer to detect every possible issue and also never issue a false alarm.
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 4월 3일
And remember, you might have a completely different save.m on your MATLAB path before the built-in routine that writes values to a file. Paths are dynamically adjustable, so this is not something that is decidable by a checker that does static code analysis.

댓글을 달려면 로그인하십시오.


Alan
Alan 2012년 4월 3일
Terminating the line with the comment
%#ok
suppresses the warning.

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by