4

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
Community
  • 1
  • 1
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77

4 Answers4

3

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

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • This leads to a warning appearing in the text editor (although for me it was mlint options) and is exactly what I wanted – Richard Tingle Feb 04 '14 at 13:27
  • 1
    Although oddly this warning now triggers for all functions, even those in the same m file – Richard Tingle Feb 04 '14 at 13:34
  • @RichardTingle Make sure you are not using them via `eval` or so. If you think the code is fine, I would recommend making a minimal example and asking a separate question. – Dennis Jaheruddin Feb 04 '14 at 14:02
  • I have created annother question regarding that [here](http://stackoverflow.com/questions/21554924/m-lint-cannot-decide-whether-name-is-a-variable-or-a-function-and-assumes-it) – Richard Tingle Feb 04 '14 at 14:17
1

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

clokep
  • 130
  • 5
  • Hmm, mlint seems to pick up on variables that are never used (which usually have warnings in the text editor as well) but `mlint('getBiggest.m')` doesn't object to the mispelled `variablename1` – Richard Tingle Feb 04 '14 at 13:23
  • You're correct by default, I just realized I have two extra messages turned on for it: "Code Analyzer cannot determine whether is a variable or a function, and assumes it is a function." and "Variable is used, but might be unset (within a script)." Those should pick up a bit more. – clokep Feb 04 '14 at 13:28
0

The Code Analyzer should catch most things like that.

Matt J
  • 1,127
  • 7
  • 15
  • The instructions for that bring up the mlint code check report but no code analyser. Perhaps this is simpy unavailable for my version of matlab – Richard Tingle Feb 04 '14 at 13:25
0

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.

Community
  • 1
  • 1
bdecaf
  • 4,652
  • 23
  • 44