Loren on the Art of MATLAB

Turn ideas into MATLAB

M-Lint Live in Editor in R2006a 15

Posted by Loren Shure,

M-Lint is a tool new in MATLAB version 7 (Release 14) to check code for possible problems and report results. You can use it from the MATLAB command line, from the Current Directory Browser, and from the MATLAB Editor. To learn more about mlint, you might find this newletter article helpful.

With the latest release, R2006a, the integration of mlint with the editor is tighter than ever. Instead of requiring you to ask for a report on your code (by selecting Check Code with M-Lint from the Tools menu), M-Lint runs all the time, continuously updating the information and messages for your code.

The reason we say that M-Lint looks for possible problems is because M-Lint can't know the coder's iintentions. Nonetheless, my experience with MATLAB leads me to suspect that certain constructs might not be what was meant. Let's look at the code in mycode.m.

function out = mymlintcode(in)
out = in;
out == -1;
out(out<0) = NaN;

Running mlint from the command line, we see this output (reformatted for better readability in the browser):

>> mlint mycode
L 1 (C 16-21): Function name 'mymlintcode' will be known to MATLAB
  by its file name: 'mycode'.
L 3 (C 5-6): Possible inappropriate use of == operator.
  Use = if assignment is intended.

We see these same messages in the editor when we hover over the orange bars on the right hand side of the editor pane. Clicking on the main orange box allows us to navigate from one message to the next. And we can fix the code as we go.

Now let's look at the messages. First, the name of the function and file don't match, and the file name will be the name that MATLAB recognizes. It's a good idea for the main function and file name to agree.

The second message is a bit more interesting. It's not wrong, but in this case, it's also not really doing anything. The output on line 3 is being calculated without an explicit left hand side, so the output goes into the variable ans. This is code is typical during debugging, especially if you are not using the integrated debugging features in MATLAB.

Moreover, I've seen multiple code examples that have lines that look like this third line, but were instead intended as an assignment statement (i.e., = was intended instead of ==). Seeing the message directly when you are creating the code, instead of doing an audit with mlint later, you are immediately able to decide how to change the code to get the result you want.

Check out some of the latest changes on the MATLAB File Exchange, where you will now see a variety of code metrics, including M-Lint results, for posted M-files.

With R2006a, you get immediate gratification about creating clean code. Check out R2006a if you haven't already for this feature and many other good ones. Does this work well for you, or do you turn off this feature by default? Add your thoughts on this topic here.


Comments are closed.

15 CommentsOldest to Newest

SRL replied on : 1 of 15

Is there anyway we can run those metrics on our own code without submiting to FEX? Specifically: percent of comments and the Cyclomatic Complexity.


Matthew Simoneau replied on : 2 of 15

SRL, we plan to make this code available for download as a submission within the next couple days.

Brendan Meade replied on : 4 of 15

The M-Lint Live in Editor…wonderful! Makes it a breeze to clean up old code without jumping back and forth between windows.

John D'Errico replied on : 5 of 15


One of the things that has irritated me about mlint is its tendency to
flag unused variables. Suppose you need only the tags that come from
a sort?

[junk , tags] = sort(x);

Mlint will flag junk as an unused variable. But since tags is used, there is
no way to get it without the first argument. I’ve even tried the construct

[tags , tags] = sort(x);

Mlint still flagged that first occurrence of tags as unused!

(This is in my old R14, SP1.)


Loren replied on : 6 of 15


I totally agree (and it’s still true in R2006a). MATLAB doesn’t have a nice way to mark unused variables and toss them nicely (the equivalent of /dev/null). I assume you’d like mlint to still tell you about other variables that truly aren’t used, like if I have code where I mistype a variable name. But these unused ones that are unavoidable right now and yet required by MATLAB are a real annoyance.


martin cohen replied on : 7 of 15

How about having a special variable name (such as “unused”) to indicate a variable that will not be used. It could be followed by any number of digits to allow multiple unused variables in the same expression.

So, the statement above could be

[unused , tags] = sort(x);

Josh replied on : 8 of 15


I like that M-Lint seems to find non-trivial code issues (like replacing prod(size(x)) with numel(x)), but I am not ready to give up using emacs for editting .m files.

What is the best way to use emacs with M-Lint? (I saw something about matlab-mode on your Newsletter, but it seemed unclear to me).


Loren replied on : 9 of 15


I am not knowledgeable about emacs (though thankfully there are some at MathWorks). Hopefully someone who does know more can add to this thread. Here‘s one matlab mode file, but I don’t know if it includes the mlint capability.

You’ll get a good link if you search for emacslink in the MATLAB documentation. It tells you how to download it and points you to installation choices, including configuring it for an integrated mlint experience.

There is a menu item in Emacs to turn on the M-Lint warnings, or you can read the
header-comments in mlint.el to see how to configure it automatically.

John D'Errico replied on : 10 of 15

Martin had an interesting idea. But instead of creating a special new variable called “unused”, why not “ans”? It is already the bit bucket that stuff gets put into anyway.


Jette replied on : 12 of 15

I like mlint in the editor – but I miss the possibility to tell mlint to ignore certain types of problems. We have a number of (partially ugly) patterns in our code which we can’t change in the near future but which produce a number of mlint-messages. It is quite difficult to find the few really useful messages in between all the messages about things we know. In my opinion, marking a line with %#ok is not a solution.

Loren replied on : 13 of 15

I agree about needing to allow mlint to be customized so users can choose which messages are important for their particular work. People here are thinking hard about that issue.

Li Dayong replied on : 14 of 15

How can configure mlint when I edit dot m file in Emacs in Linux? Every time I open a M file in Emacs, it complains that “no MLINT program available”. I find mlint in matlab/bin/glnx86, but it seems that Emacs can not use it properly.