4

Background

I have recently turned on the M-Lint warning 'M-Lint cannot decide whether ... is a variable or a function and assumes it is a function' as per Is it possible to set matlab to validate reachable functions before running in order to try to detect misspelled variable names.

M-Lint was renamed to code analyser in recent versions of matlab but I am using Matlab2007b.

Question

All functions seem to be generating this warning, even those in the same m-file. For example in the below code needlessDelegate generates this warning when used.

Is it possible to avoid this warning for valid functions? Or are my functions in some way incorrectly written?

function [biggest]=getBiggest(variable1, variable2)
    biggest=needlessDelegate(variable1, variable2); %<-- needlessDelegate generates warning. 'M-Lint cannot decide whether <name> is a variable or a function and assumes it is a function' 
end

function [biggest]=needlessDelegate(variable1, variable2)
    if variable1>variable2,
        biggest=variable1;
    else
        biggest=variable2;
    end
end

'M-Lint cannot decide whether 'needlessDelegate' is a variable or a function and assumes it is a function'

Community
  • 1
  • 1
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
  • 1
    I confirm that this still happens on `2012b` and based on [this explanation](http://www.mathworks.nl/help/matlab/matlab_prog/check-code-for-errors-and-warnings.html?s_tid=doc_12b) I believe it should not. – Dennis Jaheruddin Feb 04 '14 at 14:30
  • Well, your delegate *is* needless.... Seems to be fixed in in R2013a+ as I don't get any warnings. Does this only happen in precisely this case? What if `needlessDelegate` was a nested function or was defined in a separate M-file? – horchler Feb 04 '14 at 15:10
  • @horchler It seems to be some in-built funtions and all same m-file functions but not the primary function in an m file accessed from a script etc. – Richard Tingle Feb 04 '14 at 15:13
  • Does the R2007b version support right-clicking on the warning and and selecting "Suppress..." > {On This Line | In This File | In All Files}? – horchler Feb 04 '14 at 15:15
  • @horchler I could certainly supress these warning, in fact I only recently turned them on. But I want the positive effects of the warning "detecting things that seem not to exist" without the "triggering all the time" effects – Richard Tingle Feb 04 '14 at 15:16
  • Well, it seems like in 2007b it's not refined so you just get a lot of false positives. You could still use turn off the warnings per line or file. – horchler Feb 04 '14 at 15:19

1 Answers1

2

Your functions are not incorrectly written. However, this MLint check is not going to do what I think (from reading your other question) you want. It's not a very useful check - that's why it's off by default.

Consider that before your line

biggest=needlessDelegate(variable1, variable2);

you could have had the command load myData.mat, and the .mat file could contain a variable needlessDelegate. So until runtime, there's no way for MLint to know in general what things are functions and what things are variables.

The exception is really only when needlessDelegate is defined prior to its call - for example if you preceded your line with the command needlessDelegate = @(x,y) x+y;. You'll see then that the MLint message disappears.

As you can see, it's not a very useful check in general, which is why it's off by default.

Perhaps this example also gives an answer to your other question too - MATLAB is not able to have any idea of which things might be misspellings, or undefined variables, as you can just 'poof' things into existence at any time using load, which can't be examined before runtime.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • 1
    Hmm, this does not help my daily rage against matlab. Regardless thank you for your answer and it does make sense; I am trying to recreate my familiar statically typed environment (with all its nice checks) within a dynamically typed environment which was never likely to go well. I shall turn this warning back off and try to improve my spelling – Richard Tingle Feb 04 '14 at 15:31
  • When the rage comes, just remember that the (run->find error->fix) cycle is quicker than the (compile->run->find error->fix) cycle, even if there are a few more errors due to less strict typing. – Sam Roberts Feb 04 '14 at 15:40
  • Ah you see the problem is I've come from Java where any decent IDE is compiling continuously, there is no (visible) compile step, but the second you make a mistake you get a red underline telling you that that line is wrong. I've been spoilt by that unfortunately – Richard Tingle Feb 04 '14 at 15:44