string to calculation algorithm ?

3 ビュー (過去 30 日間)
Max Müller
Max Müller 2014 年 9 月 16 日
コメント済み: Max Müller 2014 年 9 月 16 日
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);
  1 件のコメント
Max Müller
Max Müller 2014 年 9 月 16 日
thanks

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by