localfunctions返回 m 文件中所有本地函数的函数句柄。但是,这在包中不起作用。例如,以下保存为“a.m”的代码可以正常运行:
function fs = a()
fs = localfunctions;
end
function babo()
end
function hidden()
end
从 MATLAB 控制台调用:
>> a()
ans =
@babo
@hidden
但是当它位于“+aaa/b.m”包内时,我什么也得不到:
>> aaa.b()
ans =
{}
我认为这种行为没有得到很好的记录。我该如何克服这个问题?
我需要使用 localfunctions
来 unit test包内的一些功能,我不想仅仅因为这个而将其保留在包之外。
最佳答案
一种解决方案是在调用 localfunctions
之前导入包:
+mypkg/mytest.m
function f = mytest()
import mypkg.*
f = localfunctions;
end
function foo()
end
function bar()
end
调用时:
>> f = mypkg.mytest()
f =
@foo
@bar
>> functions(f{1})
ans =
function: 'foo'
type: 'scopedfunction'
file: 'C:\Users\Amro\Desktop\+mypkg\mytest.m'
parentage: {'foo' 'mytest'}
关于matlab - 包内的`localfunctions`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25532113/