string to calculation algorithm ?

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 个评论

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.

请先登录,再进行评论。

更多回答(1 个)

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

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by