我有一个复杂的 MATLAB-Simulink 项目,涉及许多 m 文件和 mdl 文件。一些 m 文件定义了在其他 m 文件中使用的变量(我知道这是糟糕的设计,但它是遗留代码)。还有一些不再使用的功能。
我需要一种自动方式来查找未使用的变量和函数,以便我可以删除它们并使整个事情变得不那么复杂。理想情况下,我应该有一个脚本/工具,它将项目根目录的名称作为输入,扫描子目录中的所有文件,并输出任何 m 文件或 mdl 文件中未使用的所有变量和函数。
我知道我可以找到 mdl 文件中未使用的变量(请参阅 提示和技巧 - Simulink 模型中的跟踪变量)。我想将该方法应用于项目中的所有文件。
我检测 m 文件中未使用的变量的想法是暂时将所有 m 文件合并到一个文件中并运行 mlint 就可以了。还有更好的想法吗?
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 (bad design, I know, but it is legacy code). There are also functions that aren't used anymore.
I need an automatic way to find unused variables and functions so that I can delete them and make the whole thing a little less complex. Ideally I should have a script/tool which takes as input the name of root directory of the project, scans all the files in subdirectories, and outputs all the 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 that method to all the files in the project.
My idea to detect variables not used in m-files is to temporarily combine all the m-files into a single file and run mlint on it. Any better ideas?
发布评论
评论(1)
无需执行将所有 m 文件粘贴到一个文件中来运行的繁琐(且可能容易出错)的任务 MLINT,您还有其他一些选择...
如果您将所有文件都放在一个文件夹中,最简单的方法是转到当前文件夹浏览器,单击 操作按钮 ,然后选择报告 >代码分析器报告。
这将打开一个新窗口,显示当前目录中每个 m 文件的 MLINT 结果:
如果您希望使用脚本自动化该过程,而不必单击菜单选项,MathWorks 上有一些提交文件交换 (此处和此处)似乎递归地工作在目录结构上而不是在单个目录上。
另外,这里有一些示例代码,可以对单个目录执行您想要的操作:
您可以通过这种方式将文件名(和路径)的集合扩展为 在目录树上递归操作,然后对生成的文件集运行 MLINT收集。
Instead of going through the tedious (and potentially error-prone) task of pasting all of your m-files into one to run MLINT, you have a few other options...
If you have all your files in one folder, the simplest approach is to go to the Current Folder browser, click the Actions button , and then select Reports > Code Analyzer Report.
This will open a new window displaying the MLINT results for each m-file in the current directory:
If you would rather automate the process using a script instead of having to click through menu options, there are a couple of submissions on the MathWorks File Exchange (here and here) that appear to work recursively on a directory structure as opposed to just a single directory.
In additional, here is some sample code that will do what you want for a single directory:
You could extend the collection of file names (and paths) in this way to operate recursively on a directory tree, then run MLINT on the resulting set of files you collect.