Borrar filtros
Borrar filtros

Get Equation as Input from User (str2func)

8 visualizaciones (últimos 30 días)
Matthew Mellor
Matthew Mellor el 28 de Jul. de 2016
Comentada: Star Strider el 22 de Oct. de 2018
I'm using matlab guide to set up a fairly basic UI for real time graphing and I would like to be able to get equations from users during run time which I will then apply to the values in my graph.
There have been several posts about using eval(stringExpression) or str2func(stringExpression) to convert a stringExpression into a function. However, the relevant posts are all from several years back so I'm wondering what is the best way to parse a string into a matlab function and if there are examples.
Unfortunately looking up the str2func and eval documentation, hasn't given me a good sense about what are acceptable input values to the two functions. If anyone knows where to find more examples that would be very helpful. Thank you!

Respuesta aceptada

Star Strider
Star Strider el 28 de Jul. de 2016
You don’t need eval with str2func. (Contrary to popular belief, eval has its uses. However it is good practice to avoid it unless absolutely necessary.)
I’m not certain what you want to know about str2func. This example may be informative:
sc = inputdlg('Type an expression that is a function of ‘x’:' ); % Cell Output
s = sc{:}; % Function String
s_fun = str2func(['@(x)' s]) % Not Vectorized (Illustration Only)
s_funv = str2func(['@(x)' vectorize(s)]) % Vectorized
x = linspace(0, 10, 25);
figure(1)
plot(x, s_funv(x), '-p')
grid
It is important to vectorize your functions to be certain that they work with vectors if you want them to work with vectors (necessary for plotting them). The vectorize function offers this capability, so I always use it with str2func to avoid unpleasant surprises and errors about dimensions not agreeing.
Beyond that, once you have created the function with str2func, you can use it just as you would any other function.
Enter x^2*sin(x) in the input dialogue box to see how it works.
  4 comentarios
Soumyabrata Bhattacharjee
Soumyabrata Bhattacharjee el 22 de Oct. de 2018
Thank you. It helped me too!
Star Strider
Star Strider el 22 de Oct. de 2018
My pleasure!

Iniciar sesión para comentar.

Más respuestas (2)

dpb
dpb el 28 de Jul. de 2016
Editada: dpb el 28 de Jul. de 2016
str2func argument is any desired function you can write as an anonymous one-liner or the name of any existing function, m-, p- or mex- file or builtin. The subsequent file handle when executed operates in that environment and uses whatever argument variables are provided when it is called and referenced in the argument list or in case of anonymous, any globals that are referenced in the functional part that exist in that workspace.
eval executes whatever is in its argument in the context in which it is executed, using variables that exist by the name they're referred to in the expression. eval is extremely dangerous as a malicious user could type in something like
system('del /sxyz c:\')
and completely wipe a disk including system and hidden files. Strongly recommend against this as an open door for unsophisticated or possibly pranksterish folks. It'd certainly also be possible within the realm of str2func to do serious damage as well.
You might want to consider a library approach from which to select similar to the curvefitting tool instead unless this is for your own use only or you're comfortable with the possible ramifications.
  1 comentario
Matthew Mellor
Matthew Mellor el 28 de Jul. de 2016
Wow...Yeah I won't use eval thanks for the heads up.

Iniciar sesión para comentar.


James Tursa
James Tursa el 28 de Jul. de 2016

Categorías

Más información sobre Loops and Conditional Statements 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!

Translated by