convert string to equation

36 visualizaciones (últimos 30 días)
Abdullah Azzam
Abdullah Azzam el 31 de Mzo. de 2021
Comentada: per isakson el 31 de Mzo. de 2021
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?

Respuestas (1)

per isakson
per isakson el 31 de Mzo. de 2021
Editada: per isakson el 31 de Mzo. de 2021
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 comentarios
Stephen23
Stephen23 el 31 de Mzo. de 2021
Editada: Stephen23 el 31 de Mzo. de 2021
str = '5*x^2';
foo = str2func(sprintf('@(x)%s',str));
foo(2)
ans = 20
per isakson
per isakson el 31 de Mzo. de 2021
That´s better!

Iniciar sesión para comentar.

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by