Make function handlers Explcit

4 visualizaciones (últimos 30 días)
mosayyeb
mosayyeb el 4 de Dic. de 2014
Editada: Guillaume el 4 de Dic. de 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/

Respuesta aceptada

Guillaume
Guillaume el 4 de Dic. de 2014
Editada: Guillaume el 4 de Dic. de 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);

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by