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);

카테고리

Help CenterFile 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