查找 MATLAB-Simulink 项目中未使用的变量和函数

发布于 2024-10-11 22:54:06 字数 638 浏览 2 评论 0 原文

我有一个复杂的 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2024-10-18 22:54:06

无需执行将所有 m 文件粘贴到一个文件中来运行的繁琐(且可能容易出错)的任务 MLINT,您还有其他一些选择...

如果您将所有文件都放在一个文件夹中,最简单的方法是转到当前文件夹浏览器,单击 操作按钮 alt text,然后选择报告 >代码分析器报告

alt text

这将打开一个新窗口,显示当前目录中每个 m 文件的 MLINT 结果:

alt text

如果您希望使用脚本自动化该过程,而不必单击菜单选项,MathWorks 上有一些提交文件交换 (此处此处)似乎递归地工作在目录结构上而不是在单个目录上。

另外,这里有一些示例代码,可以对单个目录执行您想要的操作:

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

您可以通过这种方式将文件名(和路径)的集合扩展为 在目录树上递归操作,然后对生成的文件集运行 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 alt text, and then select Reports > Code Analyzer Report.

alt text

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

alt text

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:

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文