问题
Background
I am used to strongly typed, compiled languages so I'm used to misspellings being pretty much instantly picked up as undeclared variables.
However since Matlab is a weakly typed language this doesn't happen automatically and my development cycle tends to be:
write function(s)
|
˅
Run <-------------------------
| |
˅ |
Crash due to misspelling/typo |
| |
˅ |
Correct typo -----------------|
The run process can run for several minutes before getting to the typo, which slows down my development cycle considerably.
I'm using matlab version 2007b
Question
Is there any way to validate a function such that the use of non-existent variables etc are picked up without having to run the whole program? Given that each function has its own variable space it feels like this should be possible.
I am aware that is it possible to get a list of dependencies using depfun however I've not been able to find any way to validate those functions.
For example the following function will always fail but produces no warnings until it is run
function [biggest]=getBiggest(variableName1, variableName2)
if variablename1>variableName2, %<---misspelling!
biggest=variableName1;
else
biggest=variableName2;
end
end
回答1:
I suspect that you are either using a different editor, or that you changed your warning preferences.
When going to home > preferences > code analyzer
make sure you have the one enabled that contains something like:
cannot determine whether ... is a variable or a function
回答2:
The MATLAB Linter will generally pick up variables that are used before being assigned (e.g. because it's a typo), but it isn't perfect. It is enabled by default (in R2011b, at least) in the GUI but can also be run outside of MATLAB: http://www.mathworks.com/help/matlab/ref/mlint.html
回答3:
The Code Analyzer should catch most things like that.
回答4:
Personally I would create some unit tests. I use xUnit, but there is a whole question dedicated to it: Unit-testing framework for MATLAB.
For sure it will catch syntax errors. In addition it helps checking the algorithm.
来源:https://stackoverflow.com/questions/21553530/is-it-possible-to-set-matlab-to-validate-reachable-functions-before-running