Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

M-Lint Live in Editor in R2006a

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.


  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.