Make function handlers Explcit
1 view (last 30 days)
Show older comments
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/
0 Comments
Accepted Answer
Guillaume
on 4 Dec 2014
Edited: Guillaume
on 4 Dec 2014
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 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!