Make function handlers Explcit

1 Ansicht (letzte 30 Tage)
mosayyeb
mosayyeb am 4 Dez. 2014
Bearbeitet: Guillaume am 4 Dez. 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/

Akzeptierte Antwort

Guillaume
Guillaume am 4 Dez. 2014
Bearbeitet: Guillaume am 4 Dez. 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);

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by