file - MatLab - 根据名称执行函数

标签 file matlab

我有几个MatLab函数,几乎所有函数都有测试函数。目前测试函数还没有真正的命名约定,因此我最终得到了诸如 test_functionNametests_functionNameFunctionName_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/

相关文章:

python - 如何在 Python 中确定打开文件的大小?

django - 删除Django中的空目录

matlab - 在 MATLAB 中从最小到最大对元胞数组的所有维度进行排序

algorithm - 排序算法 : Big text file with variable-length lines (comma-separated values)

file - 减少 FORTRAN 中输出文件的大小

file - 如何将 Artifactory 上传到 Artifactory/在 Artifactory 没有扩展名的构建系统 (Gradle Maven Ant) 中使用它

c++ - 将 C++ 中的多个参数传递给 MatLab 共享库函数

class - 复制作为句柄类的类的方法?

matlab - 在 MATLAB 的抽象类中声明 function_handle 类型的属性的正确方法是什么?

python - Vpython 中的 Matlab 冒号运算符等效项