string to calculation algorithm ?

3 vues (au cours des 30 derniers jours)
Max Müller
Max Müller le 16 Sep 2014
Commenté : Max Müller le 16 Sep 2014
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 commentaire
Stephen23 le 16 Sep 2014
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.

Connectez-vous pour commenter.

Réponse acceptée

Michael Haderlein
Michael Haderlein le 16 Sep 2014

Plus de réponses (1)

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


En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by