Possible to use anonymous function as script function input?
Mostrar comentarios más antiguos
Hey everyone!
I was wondering if there is any method to allow anonymous function to be used as an input. For example:
I have the following script:
function output = exampleF(input)
output = 3*input
And then use sin(x) as an input:
exampleF(sin(x))
By doing it like that I would get that x is not a defined variable.
Is there any way to program 'sin(x)' as an anonymous function ? in a same manner as
f = @(x) sin(x).
Thank you for your time!
Respuesta aceptada
Más respuestas (2)
Walter Roberson
el 15 de Oct. de 2012
exampleF(@(x) sin(x))
However, you will find that multiplying a function handle by a constant is not allowed.
Is your desired output a numeric value or a new function handle ?
2 comentarios
Kokalz
el 15 de Oct. de 2012
Walter Roberson
el 15 de Oct. de 2012
Yes if you include the apostrophes, such as
exampleF('sin(x)')
Equivalent would be using command form,
exampleF sin(x)
but that would not be able to return a value to a variable.
It is not possible to write
exampleF(sin(x))
without having the sin(x) be evaluated before being passed in to exampleF
per isakson
el 15 de Oct. de 2012
Editada: per isakson
el 15 de Oct. de 2012
0 votos
Categorías
Más información sobre Mathematics en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!