我有几个MatLab函数,几乎所有函数都有测试函数。目前测试函数还没有真正的命名约定,因此我最终得到了诸如 test_functionName
、tests_functionName
、FunctionName_Test
等函数。
但是,我发现这些函数有两个共同点:
- 名称包含“test”(大小写不同)。
- 它们没有输入或输出参数。
我想编写一个函数,它可以在给定文件夹(或路径)下查找所有符合这两个条件的函数并执行它们。这样我就可以在一次调用中执行所有测试函数。
有什么办法可以做到这一点吗?
最佳答案
您可以执行以下操作:
fun=dir('*test*.m'); %% look for matlab scripts which name contains 'test'
fun={fun.name}; %% extract their names
fun=fun(cellfun(@(x) (nargin(x)==0),fun)); %% select the ones with no input arguments
fun = regexprep(fun, '.m', ''); % remove '.m' from the filenames
cellfun(@eval,fun); %% execute them
关于file - MatLab - 根据名称执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12752147/