Make function handlers Explcit

1 view (last 30 days)
mosayyeb
mosayyeb on 4 Dec 2014
Edited: Guillaume on 4 Dec 2014
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/

Accepted Answer

Guillaume
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);

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!