I do not believe that you can add new code templates for MATLAB Code Analyzer for search. All you can do is set which existing alerts are displayed or suppressed.
I'm not sure which third-party tools can be used to analyze the code, and creating a general-purpose analyzer will be quite difficult. However, if there were some very specific, well-defined patterns that you would like to try and highlight in your code, you can try to parse it using regular expressions (screaming music and screams).
This is often difficult, but doable. As an example, I wrote this piece of code that is looking for the template mentioned above. One of the things that often need to be managed when doing something like this is the set of sets of enclosing parentheses that I process by first removing the uninteresting pairs of parentheses and their contents:
function check_code(filePath) % Read lines from the file: fid = fopen(filePath, 'r'); codeLines = textscan(fid, '%s', 'Delimiter', '\n'); fclose(fid); codeLines = codeLines{1}; % Remove sets of parentheses that do not encapsulate a logical statement: tempCode = codeLines; modCode = regexprep(tempCode, '\([^\(\)<>=~\|\&]*\)', ''); while ~isequal(modCode, tempCode) tempCode = modCode; modCode = regexprep(tempCode, '\([^\(\)<>=~\|\&]*\)', ''); end % Match patterns using regexp: matchIndex = regexp(modCode, 'numel\([^\(\)]+[<>=~\|\&]+[^\(\)]+\)'); % Format return information: nMatches = cellfun(@numel, matchIndex); index = find(nMatches); lineNumbers = repelem(index, nMatches(index)); fprintf('Line %d: Potential incorrect use of NUMEL in logical statement.\n', ... lineNumbers); end % Test cases: % if numel(list < x) % if numel(list) < x % if numel(list(:,1)) < x % if numel(list(:,1) < x) % if (numel(list(:,1)) < x) % if numel(list < x) & numel(list < y) % if (numel(list) < x) & (numel(list) < y)
Note. I added some test cases in the comments at the bottom of the file. When I run this code on its own, I get the following:
>> check_code('check_code.m') Line 28: Potential incorrect use of NUMEL in logical statement. Line 31: Potential incorrect use of NUMEL in logical statement. Line 33: Potential incorrect use of NUMEL in logical statement. Line 33: Potential incorrect use of NUMEL in logical statement.
Please note that for the first, fourth and sixth test cases, a message is indicated that matches your error code (twice for the sixth test case, since there are two errors in this line).
Will this work in all possible situations? I would suggest no. You may need to increase the complexity of regex patterns to handle additional situations. But at least this can serve as an example of what you need to consider when analyzing code.