string to calculation algorithm ?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
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.
채택된 답변
추가 답변 (1개)
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 Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!