Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to fix all MATLAB mlint messages at once?

Tags:

matlab

mlint

I've inherited some code where the author had an aversion to semicolons. Is it possible to fix all the mlint messages in one go (at least all the ones with an automatic fix), rather than having to click each one and press ALT+ENTER?

like image 562
Richie Cotton Avatar asked Jul 14 '10 16:07

Richie Cotton


People also ask

How do I fix errors in Matlab?

If you are unfamiliar with the problem, right-click the highlighted code. The first item in the context menu shows the suggested fix. Select the item to apply the fix. If multiple instances of a problem exist, MATLAB might offer to apply the suggested fix for all instances of the problem.

What is Mlint Matlab?

mlint('lengthofline') MATLAB displays the M-Lint messages for lengthofline. m in the Command Window: L 22 (C 1-9): The value assigned here to variable 'nothandle' might never be used. L 23 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).


1 Answers

NOTE: This answer uses the function MLINT, which is no longer recommended in newer versions of MATLAB. The newer function CHECKCODE is preferred, and the code below will still work by simply replacing the call to MLINT with a call to this newer function.


I don't know of a way in general to automatically fix code based on MLINT messages. However, in your specific case there is an automated way for you to add semicolons to lines that throw an MLINT warning.

First, let's start with this sample script junk.m:

a = 1
b = 2;
c = 'a'
d = [1 2 3]
e = 'hello';

The first, third, and fourth lines will give you the MLINT warning message "Terminate statement with semicolon to suppress output (within a script).". Using the functional form of MLINT, we can find the lines in the file where this warning occurs. Then, we can read all the lines of code from the file, add a semicolon to the ends of the lines where the warning occurs, and write the lines of code back to the file. Here's the code to do so:

%# Find the lines where a given mlint warning occurs:

fileName = 'junk.m';
mlintID = 'NOPTS';                       %# The ID of the warning
mlintData = mlint(fileName,'-id');       %# Run mlint on the file
index = strcmp({mlintData.id},mlintID);  %# Find occurrences of the warnings...
lineNumbers = [mlintData(index).line];   %#   ... and their line numbers

%# Read the lines of code from the file:

fid = fopen(fileName,'rt');
linesOfCode = textscan(fid,'%s','Delimiter',char(10));  %# Read each line
fclose(fid);

%# Modify the lines of code:

linesOfCode = linesOfCode{1};  %# Remove the outer cell array encapsulation
linesOfCode(lineNumbers) = strcat(linesOfCode(lineNumbers),';');  %# Add ';'

%# Write the lines of code back to the file:

fid = fopen(fileName,'wt');
fprintf(fid,'%s\n',linesOfCode{1:end-1});  %# Write all but the last line
fprintf(fid,'%s',linesOfCode{end});        %# Write the last line
fclose(fid);

And now the file junk.m should have semicolons at the end of every line. If you want, you can put the above code in a function so that you can easily run it on every file of your inherited code.

like image 77
gnovice Avatar answered Oct 12 '22 21:10

gnovice