How to use input string as a function in Appdesign?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Abrar
el 21 de Feb. de 2023
Comentada: Abrar
el 21 de Feb. de 2023
I am beginner in appdesigning. However, I am trying here to make an interface where user will write down the equation of a function and the value of the variable. After clicking the button,he will get the value of f(x) for the given value. Maybe I have some errors in the codes for which,after giving datas and clicking then,there seems something wrong. Someone please help me to debug this I need that emergency.
0 comentarios
Respuesta aceptada
Simon Chan
el 21 de Feb. de 2023
Editada: Simon Chan
el 21 de Feb. de 2023
In case you would like to use function str2func, the string in the editfield for entering the equation should looks like the following:
@(x)x^2
instead of
x^2
and use the following inside the callback
f = str2func(s)
instead of
f = @(x)(str2func(s))
Más respuestas (1)
Steven Lord
el 21 de Feb. de 2023
If the user enters an expression in the edit field, you can convert it into an anonymous function using a couple of functions.
s = 'x^2+y';
Make it so it can be called with a vector of values using the vectorize function.
vecFunction = vectorize(s)
Get the list of variables using symvar.
vars = symvar(s)
Assemble the string representation of the anonymous function, joining the list of variables and the function that was entered into the edit box.
str = "@(" + join(vars, ", ") + ") " + vecFunction
Make the anonymous function with str2func.
f = str2func(str)
To check let's call the function handle.
z = f(1:10, 100)
0 comentarios
Ver también
Categorías
Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!