M-Lint Code Check Report

Running the M-Lint Code Check Directory Report

The M-Lint Code Check Report displays potential errors and problems, as well as opportunities for improvement in your code. The term "lint" is the name given to similar tools used with other programming languages such as C. M-Lint produces a message for each line of an M-file that it determines might be improved. For example, a common M-Lint message is that a variable foo in line 12 is defined but never used in the M-file.

To run the M-Lint code check directory report, follow these steps:

  1. In the Current Directory browser, navigate to the directory that contains the M-files you want to check with M-Lint. To use the example shown in this documentation, lengthofline.m, you can change the current directory by running

    cd(fullfile(matlabroot,'help','techdoc','matlab_env','examples'))
  2. If you plan to modify the example, save the file to a directory for which you have write access, and then make that directory the current MATLAB® directory. In this example, the file is saved to I:\MATLABFiles\mymfiles.

  3. In the Current Directory browser toolbar, select the M-Lint Code Check Report from the Directory Reports listing—for details, see Accessing and Using Directory Reports.

    The M-Lint Code Check Report displays in the MATLAB Web Browser, showing those M-files that M-Lint identified as having potential problems or opportunities for improvement.

    Image of M-Lint report.

  4. For each message, review the suggestion and your code, click the line number to open the M-file in the Editor at that line, and make changes based on the message. Use the following general advice:

  5. After making changes, save the M-file. Consider saving the file to a different name if you made significant changes that might introduce errors. Then you can refer to the original file if needed to resolve problems with the updated file. Use Tools > Compare Against in the Editor to help you identify the changes you made to the file. For more information, see Comparing Files and Directories.

  6. Run and debug the file(s) again to be sure you have not introduced any inadvertent errors.

  7. If the M-Lint Code Check Report is already displayed, click Rerun This Report to update the report based on the changes you made to the file, or run the report from the Current Directory browser toolbar. Ensure the M-Lint messages are gone, based on the changes you made to the M-files.

Making Changes Based on M-Lint Messages

For information on how to correct the potential problems presented by M-Lint, use the following resources:

Other techniques to help you identify problems in and improve your M-files are in these topics:

Example Using M-Lint Messages to Improve Code

An example file, lengthofline.m, is included with the MATLAB product in matlabroot/matlab/help/techdoc/matlab_env/examples.

To run the M-Lint Code Check Report for lengthofline.m, change the current directory to its location by running

cd(fullfile(matlabroot,'help','techdoc','matlab_env','examples'))

In the Current Directory browser, select the M-Lint Code Check Report from the list of directory reports on the toolbar.

The M-Lint Code Check Report appears, with its list of messages suggesting improvements you can make to lengthofline.m and any other files in the directory.

Image of M-Lint Code Check report for the sample M-file, lengthofline.m.

Messages and Resulting Changes for the lengthofline Example.   The following table describes each message and demonstrates a way to change the file, based on the message.

Message — Code (Original Line Numbers)

Explanation and Updated Code (New Line Numbers)

22: The value assigned here to variable 'nothandle' might never be used.

— — — — — — — — — — — — — — — — —

22 nothandle = ~ishandle(hline);

23 for nh = 1:prod(size(hline))

24 notline(nh) = ~ishandle(hline(nh)) ...

In line 22, nothandle is assigned a value, but nothandle is probably not used anywhere after that in the file. The line might be extraneous and you could delete it. But it might be that you actually intended to use the variable, which is the case for the lengthofline example. Update line 24 to use nothandle, which is faster than computing ~ishandle for each iteration of the loop, as shown here.

22 nothandle = ~ishandle(hline);

23 for nh = 1:numel(hline)

24 notline(nh) = nothandle(nh) ...

23: NUMEL(x) is usually faster than PROD(SIZE(x)).

— — — — — — — — — — — — — — — — —

23 for nh = 1:prod(size(hline))

While prod(size(x)) returns the number of elements in a matrix, the numel function was designed to do just that, and therefore is usually more efficient. Type doc numel to see the numel reference page. Change the line to

23 for nh = 1:numel(hline)

24: 'notline' might be growing inside a loop. Consider preallocating for speed.

— — — — — — — — — — — — — — — — —

22 nothandle = ~ishandle(hline);

23 for nh = 1:numel(hline)

24 notline(nh) = ~ishandle(hline(nh)) ...

When you increase the size of an array within a loop, it is inefficient. Before the loop, preallocate the array to its maximum size to improve performance. For more information, see Preallocating Memory in the MATLAB Programming documentation. In the example, add a new line to preallocate notline before the loop.

23 notline = false(size(hline));

24 for nh = 1:numel(hline)

25 notline(nh) = nothandle(nh) ...

24: Use STRCMPI(str1,str2) instead of using LOWER in a call to STRCMP.

— — — — — — — — — — — — — — — — —

24 notline(nh)=~ishandle(hline(nh)) || ~strcmp('line',lower(get(hline(nh), 'type')));

While

strcmp ('line',lower(get(hline(nh)'type'))

converts the result of the get function to a lowercase string before doing the comparison, the strcmpi function ignores the case while performing the comparison, with advantages that include more efficiency. Change line 25 to

notline(nh) = nothandle(nh) || ~strcmpi('line',get(hline(nh),'type'));

28: NUMEL(x) is usually faster than PROD(SIZE(x)).

— — — — — — — — — — — — — — — — —

28 for nl = 1:prod(size(hline))

See the same message and explanation reported for line 23. Change the line 29 to

for nl = 1:numel(hline)

34: 'data' might be growing inside a loop. Consider preallocating for speed.

— — — — — — — — — — — — — — — — —

33 for nd = 1:length(fdata)

34 data{nd} = getfield(flds,fdata{nd});

See the same message and explanation reported for line 24. Add this line, 34, before the loop

data = cell(size(fdata));

34: Use dynamic fieldnames with structures instead of GETFIELD. Type 'doc struct' for more information.

— — — — — — — — — — — — — — — — —

34 data{nd} = getfield(flds,fdata{nd});

You can access a field in a structure as a variable expression that MATLAB evaluates at run-time. This is more efficient than using getfield. For more information, type doc struct to see the reference page for structures, or see Using Dynamic Field Names in the MATLAB Programming documentation. Change line 37 to

data{nd} = flds.(fdata{nd});

38: Use || instead of | as the OR operator in (scalar) conditional statements.

39: Use || instead of | as the OR operator in (scalar) conditional statements.

40: Use || instead of | as the OR operator in (scalar) conditional statements.

— — — — — — — — — — — — — — — — —

38 if isempty(data{3}) | ...

39 (length(unique(data{1}(:)))==1 | ...

40 length(unique(data{2}(:)))==1 | ...

41 length(unique(data{3}(:)))==1)

While | (the element-wise logical OR operator) performs the comparison correctly, use the || (short circuit OR operator) for efficiency. For details, see Logical Operators in the MATLAB Programming documentation. Change lines 40, 41, and 42 to

if isempty(data{3}) || ...

(length(unique(data{1}(:)))==1 || ...

length(unique(data{2}(:)))==1 || ...

42: 'data' might be growing inside a loop. Consider preallocating for speed.

— — — — — — — — — — — — — — — — —

42 data{3} = zeros(size(data{1}));

This message no longer appears due to the change made to line 34 data{nd} = getfield(flds,fdata{nd});. Sometimes fixing code in one line automatically clears a message for another line. If the reason for a message or the action to take for a message is not obvious at first, it could be because another line is causing the message. Address the issues that are easy to fix first and rerun the report. Do not make any changes to line 44.

43: 'dim' might be growing inside a loop. Consider preallocating for speed.

43 dim(nl) = 2;

See the same message and explanation reported for line 24. Add this line before the first line of the loop

dim = len;

48: There may be a parenthesis imbalance around here.

48: There may be a parenthesis imbalance around here.

48: There may be a parenthesis imbalance around here.

48: There may be a parenthesis imbalance around here.

There is an error in this line, which you would see by running lengthofline. M-Lint suggests that it might be due to a parenthesis imbalance. You can check that by moving the arrow key over each of the delimiters, to see if MATLAB indicates a mismatch. This requires that File > Preferences > Keyboard > Delimiter Matching has the Match on arrow key option selected. There are no mismatched delimiters. The actual problem is the semicolon in parentheses, data{3}(:) is incorrect and should be a colon. In line 51, change data{3}(;) to data{3}(:). That single change addressed the issues in all the messages for that line.

49: Terminate statement with semicolon to suppress output (in functions).

Adding a semicolon to the end of a statement suppresses output and is a common practice. M-Lint alerts you to lines that produce output but lack the terminating semicolon. If you want to view output from this line, do not add the semicolon. You can instruct M-Lint to ignore all messages on this line so that the messages on it will not appear by adding %#ok to the end of the line. However, because there is currently another message on the line, do not add %#ok until you have addressed the other message.

Alternatively, you can add %#ok with the message ID for the specific message you want to suppress. To determine the message ID, run mlint('lengthofline.m', '-id'), which indicates the ID is NOPRT—for more information, see the mlint function reference page.

For this example, assume you want to display the output and suppress the M-Lint message. To do so, add %#ok<NOPRT> to the end of the line.

Note that there is a similar message for M-file scripts. This is so you can suppress the message for M-files that are cell-mode scripts, because they are often intended as demos and the display of output is intentional.

49: Use of brackets [] is unnecessary. Use parentheses to group, if needed.

— — — — — — — — — — — — — — — — —

49 len(nl) = sum([sqrt(dot(temp',temp'))])

For more information about the use of brackets and parentheses, see the Special Characters reference page. In this example, remove the brackets because they are not needed. They add processing time because MATLAB concatenates unnecessarily. Change line 52 to

len(nl) = sum(sqrt(dot(temp',temp'))) %#ok

Image of M-Lint report after changing the sample file, lengthofline.m, based on M-Lint messages. No M-Lint messages are reported.

The M-file that includes all of the changes suggested by M-Lint is lengthofline2.m. To view it, run

edit(fullfile(matlabroot,'help','techdoc','matlab_env',... 'examples','lengthofline2.m')).

Other Ways to Access M-Lint

You can get M-Lint messages using any of the following methods. Each provides the same M-Lint messages, but in a different format:

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS