string to calculation algorithm ?

4 visualizzazioni (ultimi 30 giorni)
Max Müller
Max Müller il 16 Set 2014
Commentato: Max Müller il 16 Set 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 Commento
Stephen23
Stephen23 il 16 Set 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.

Accedi per commentare.

Risposta accettata

Michael Haderlein
Michael Haderlein il 16 Set 2014
eval(string)

Più risposte (1)

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

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by