convert string to equation

조회 수: 32 (최근 30일)
Abdullah Azzam
Abdullah Azzam 2021년 3월 31일
댓글: per isakson 2021년 3월 31일
Hi guys I am building an application using the app designer. However, I have ran into issue because I want the user to feed his equation into the app. However, the edited field can either be numaric or text so if fore example user want to input an equation (5*x^2) how would that be done? I have tried using string field and use str2double and str2num but that didn't work any thoughts on that?

답변 (1개)

per isakson
per isakson 2021년 3월 31일
편집: per isakson 2021년 3월 31일
Or a smelling solution if you don't have the symbolic toolbox
>> x=3;
>> v = eval('(5*x^2)')
v =
45
>>
Create an anonymous function
>> foo = eval(['@(x)','(5*x^2)'])
foo =
function_handle with value:
@(x)(5*x^2)
>> foo(2)
ans =
20
>>
  댓글 수: 2
Stephen23
Stephen23 2021년 3월 31일
편집: Stephen23 2021년 3월 31일
str = '5*x^2';
foo = str2func(sprintf('@(x)%s',str));
foo(2)
ans = 20
per isakson
per isakson 2021년 3월 31일
That´s better!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by