can mlint report undefined variables?

Is there a way to tell mlint, or is there some other way, to report on unknown identifiers in functions. For example,
function r = test_unknown_variables(x,y)
r = w + v;
end
>> mlint('test_unknown_variables')
L 1 (C 38): Input argument 'x' might be unused. If this is OK, consider replacing it by ~.
L 1 (C 40): Input argument 'y' might be unused. If this is OK, consider replacing it by ~.
It would be nice if mlint had a "strict" mode that reported that w and v are undefined. I know I might later define functions w and v, but basically the way I program, 100% of the time it's a mistake on my part if I have undefined variables.

 Accepted Answer

The problem is that w and v could be m-file functions. There is an optional test that you can apply in the File | Preferences | Code Analyzer.
>> msg = mlint('H:\m\cssm\test_unknown_variables.m')
msg =
2x1 struct array with fields:
message
line
column
fix
>> msg(2).message
ans =
Code Analyzer cannot determine whether 'v' is a variable or a function, and assumes it is a function.

3 Comments

That does the trick....except it warns about built-ins like error, length,isempty,strcmp,regexpi,zeros!!!!
function r = test_unknown_variables(x,y)
z = zeros(1,5);
w = length(x);
r = strcmp(x,y);
end
>> mlint('test_unknown_variables')
L 2 (C 2): The value assigned to variable 'z' might be unused.
L 2 (C 6-10): M-Lint cannot determine whether 'zeros' is a variable or a function, and assumes it is a function.
L 3 (C 2): The value assigned to variable 'w' might be unused.
L 3 (C 6-11): M-Lint cannot determine whether 'length' is a variable or a function, and assumes it is a function.
L 4 (C 6-11): M-Lint cannot determine whether 'strcmp' is a variable or a function, and assumes it is a function.
}
Yes, it would be more useful if mlint tests for built-in and m-files on the search path.
I would think a little regex foo could pull out the potential function names. Then you could use which to test if it is a function. No built in way, but definitely feasible.

Sign in to comment.

More Answers (0)

Categories

Products

Tags

Asked:

Tom
on 28 Sep 2012

Community Treasure Hunt

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

Start Hunting!