MATLAB:从“命令行"确定依赖项,不包括内置依赖项 [英] MATLAB: determine dependencies from 'command line' excluding built in dependencies

查看:113
本文介绍了MATLAB:从“命令行"确定依赖项,不包括内置依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用脚本(命令行)中的命令确定.m文件的所有依赖关系以及它调用的文件的任何依赖关系?

Is there a way to determine all the dependencies of an .m file and any of the dependencies of the files it calls using a command in a script (command-line)?

以前有一个类似的问题,它非常好,因为它建议使用depfun函数.但与此有关的问题是,它也输出了与之相关的 MATLAB 相关文件

There was a question like this before and it was really good because it suggested using the depfun function. BUT the issue with this was that it is outputting the MATLAB related files that it depends on as well.

示例: testing.m

EXAMPLE: testing.m

disp('TESTING!!');

depfun('testing')的输出

The output of depfun('testing')

'C:\testing.m'
'C:\MATLAB\R2008a\toolbox\matlab\datatypes\@opaque\char.m'
'C:\MATLAB\R2008a\toolbox\matlab\datatypes\@opaque\double.m'
'C:\MATLAB\R2008a\toolbox\matlab\datatypes\@opaque\toChar.m'
'C:\MATLAB\R2008a\toolbox\matlab\elfun\log10.m'
'C:\MATLAB\R2008a\toolbox\matlab\elmat\ans.m'

列表更长一些.

这里的意思是我希望会有一些类似的功能或一个标记来删除这些不需要的依赖项.

The point here is that I was hoping there would be some similar function or a flag that would remove these unwanted dependencies.

推荐答案

当我编写一个简单的函数来创建

Here are a couple of links I found helpful when I wrote up a simple function to create a table of contents for an m-file:

  • A thread discussing the undocumented function MLINTMEX
  • FDEP by Urs Schwarz on the MathWorks File Exchange
  • FARG by Urs Schwarz on the MathWorks File Exchange

由于此问题激起了我的好奇心,所以我开始尝试几种可能的解决方法.查找对非工具箱.m和.mex文件的依赖关系比较简单(我在MATLAB版本7.1.0.246中做到了):

Since this problem piqued my curiosity, I started trying out a few ways I might approach it. Finding the dependencies on non-toolbox .m and .mex files was relatively trivial (I did this in MATLAB version 7.1.0.246):

fcnName = 'myfile.m';
fcnList = depfun(fcnName,'-quiet');
listIndex = strmatch('C:\Program Files\MATLAB71\toolbox',fcnList);
fcnList = fcnList(setdiff(1:numel(fcnList),listIndex));

在这里,我只是使用 DEPFUN 来获取依赖项,然后删除所有以"C:\ Program Files \ MATLAB71 \ toolbox"开头的文件,其中MATLAB工具箱位于我的计算机上.请注意,这是假设您没有在这些MATLAB目录中放置任何自己的代码(无论如何都不应该这样做).

Here, I just used DEPFUN to get the dependencies, then I removed any files that began with 'C:\Program Files\MATLAB71\toolbox', where the MATLAB toolboxes are located on my machine. Note that this assumes you aren't placing any of your own code in these MATLAB directories (which you shouldn't do anyway).

为了获得对.mat和.txt文件的依赖性,我采用了另一种方法.对于从上述代码中获得的每个文件,可以将文件的文本加载到MATLAB中,并使用正则表达式进行解析,以找到以'.mat'或'.txt'结尾的字符串:

To get dependencies on .mat and .txt files, I took another approach. For each of the files you get from the above code, you could load the text of the file into MATLAB and parse it with a regular expression to find strings that end in a '.mat' or '.txt':

fid = fopen(fcnName,'rt');
fcnText = fscanf(fid,'%c');
fclose(fid);
expr = '[^\'']\''([^\''\n\r]+(?:\w\.(?:mat|txt)){1})\''[^\'']';
dataFiles = regexp(fcnText,expr,'tokens');
dataFiles = unique([dataFiles{:}]).';

我使用的正则表达式有一些限制:

There are a few limitations to the regular expression I used:

  • 如果注释中出现了类似"help.txt"的字符串(例如函数的帮助注释块),则正则表达式仍会检测到该字符串.我尝试使用环视操作符解决此问题,但运行时间太长.

  • If you have a string like 'help.txt' that appears in a comment (such as the help comment block of a function), it will still be detected by the regular expression. I tried to get around this with a lookaround operator, but that took too long to run.

如果从变量构建字符串(例如"fileString = [someString'.mat']"),则正则表达式将不会检测到该字符串.

If you build a string from variables (like "fileString = [someString '.mat']"), it will not be detected by the regular expression.

返回的文件名字符串将是相对路径字符串.换句话说,如果函数中包含字符串"help.txt"或"C:\ temp \ junk.mat",则正则表达式匹配将返回"help.txt"或"C:\ temp \ junk.mat" ',与它们在函数中显示的完全相同.要查找完整路径,可以使用 WHICH 对每个数据文件起作用(假设文件位于MATLAB路径中的某个位置).

The returned strings of file names will be relative path strings. In other words, if you have the strings 'help.txt' or 'C:\temp\junk.mat' in the function, the regular expression matching will return 'help.txt' or 'C:\temp\junk.mat', exactly as they appear in the function. To find the full path, you can use the WHICH function on each data file (assuming the files reside somewhere on the MATLAB path).

希望您发现这些有用! =)

Hope you find these useful! =)

这篇关于MATLAB:从“命令行"确定依赖项,不包括内置依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆