Get Equation as Input from User (str2func)

6 次查看(过去 30 天)
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!

采纳的回答

Star Strider
Star Strider 2016-7-28
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 个评论

请先登录,再进行评论。

更多回答(2 个)

dpb
dpb 2016-7-28
编辑:dpb 2016-7-28
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.

James Tursa
James Tursa 2016-7-28

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by