Make function handlers Explcit

1 次查看(过去 30 天)
Hi, I have some function handlers (S=@(x)x , R=@(x)x^2). I make a calculation on them and store it as a new function handler (J=@(x) S+R). I need explicit expression of function handle J (e.g J=@(x) x+x^2 not J=@(x) S(x)+R(x) ). Any suggestion/

采纳的回答

Guillaume
Guillaume 2014-12-4
编辑:Guillaume 2014-12-4
This may work, as long as both function handles use the same variable name:
varexps = regexp(func2str(S), '@\((.*?))(.*)', 'tokens', 'once');
varexpr = regexp(func2str(R), '@\((.*?))(.*)', 'tokens', 'once');
assert(strcmp(varexps{1}, varexpr{1}), 'variable name mismatch');
j = str2func(sprintf('@(%s) %s+%s', varexps{1}, varexps{2}, varexpr{2}));
However, I don't see why you'd want to do that in the first place. There shouldn't be any functional difference between that and
J = @(x) S(x)+R(x);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by