仿照python的装饰器https://www.runoob.com/w3cnote/python-func-decorators.html,本人写了一个matlab的装饰器:
info.m
function result_func= info(msg)
function res_func =wrap(func)
function varargout = inner_wrap(varargin)
varargout= num2cell(zeros(1,nargout));
disp(['start ',msg])
[varargout{:}] = func(varargin{:});
disp(['end ',msg])
end
res_func = @inner_wrap;
end
result_func = @wrap;
end
ff.m
function varargout = ff(varargin)
disp('run ff');
for i=1:length(varargin)
varargout{i} = varargin{i} + 1;
end
end
执行脚本:
g=info('func');
h=g(@ff);
a=h(1)
[a,b]=h(2,3)
[a,b,c]=h(3,4,5)
运行结果:
不过该函数装饰器有个缺点就是不能覆盖原来的函数,没有像python一样的@语法糖