Find unused variables and functions in a MATLAB-Simulink project

I have a complex MATLAB-Simulink project involving many m files and mdl files. Some m files define variables that are used in other m files (poor design, I know, but this is outdated code). There are also functions that are no longer used.

I need an automatic way to search for unused variables and functions so that I can remove them and make it all a little less complicated. Ideally, I should have a script / tool that accepts the project root directory, scans all files in subdirectories and displays all variables and functions that are not used in any m file or mdl file.

I know that I can find variables that are not used in mdl files (see Tips and Tricks - Tracking Variables in a Simulink Model ), I would like to apply this method to all files in a project.

My idea of ​​finding variables not used in m files is to temporarily merge all m files into one file and run mlint on it. Any better ideas?

+6
matlab simulink
source share
1 answer

Instead of going through the tedious (and potentially risky) task of inserting all your m files into one to run MLINT , you have a few more options ...

If you have all your files in one folder, the easiest way is to go to the browser of the current folder, click Actions alt text , and then select Reports> Report Code Analyzer .

alt text

This will open a new window displaying the MLINT results for each m file in the current directory:

alt text

If you prefer to automate the process using a script instead of clicking on the menu options, there are several views on MathWorks file sharing ( here and here ) that seem to work recursively in the directory structure, and not just in one directory.

In addition, here is an example of code that will do what you want for a single directory:

dirData = dir; %# Get data on the current directory contents fileIndex = ~[dirData.isdir]; %# Get an index for the files fileNames = {dirData(fileIndex).name}; %# Get the file names [~,~,ext] = cellfun(@fileparts,fileNames,... %# Get the file extensions 'UniformOutput',false); mFileIndex = strcmp(ext,'.m'); %# Get an index for the m-files cellfun(@mlint,fileNames(mFileIndex)); %# Run MLINT on each m-file 

You can expand the collection of file names (and paths) so that they work recursively in the directory tree , then run MLINT in the resulting set of files that you collect.

+7
source share

All Articles