string to calculation algorithm ?

5 次查看(过去 30 天)
Hey Guys, Imagine u have a GUI which hold the variable x = 1:10 (doubble). Now I want to create an Editbox which allows the user to enter a calculation algorithm.
for example: x*3.
Is it possible to convert this string (x*3) into an actual calculation algorithm ?
  1 个评论
Stephen23
Stephen23 2014-9-16
The answers below give some solutions, including using eval and str2func , but keep in mind these could be dangerous as this allows the user to run arbitrary code, including system calls via MATLAB.

请先登录,再进行评论。

采纳的回答

Michael Haderlein
Michael Haderlein 2014-9-16
eval(string)

更多回答(1 个)

Adam
Adam 2014-9-16
You can also create a function handle if you want, something like:
s = 'x*3';
s = ['@(x)' s];
f = str2func(s);
f(x);

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by