Borrar filtros
Borrar filtros

How to replace the string using strrep?

2 visualizaciones (últimos 30 días)
Kratos
Kratos el 18 de Feb. de 2015
Comentada: Star Strider el 18 de Feb. de 2015
say I have a str = 'f(x) = 2*x^3' or 'fun(var_name) = 2*var_name^3'
and I have a value of 3(val = 3)
how do I make sure that the strrep will replace the x value or var_name with the value that I want?

Respuesta aceptada

Star Strider
Star Strider el 18 de Feb. de 2015
I am not certain what you want to do, but if you want to make functions out of your expressions, use the Anonymous Functions syntax:
f = @(x) 2*x.^3;
then call them as you would any other function:
var_name = 5;
result = f(var_name)
produces:
result =
250
  2 comentarios
Kratos
Kratos el 18 de Feb. de 2015
What I meant was I don't know the I don't know what the input is going to be it could be anything like 'fun(var_name) = 2*var_name^3' or 'fun(pos) = 2*pos^3' or 'fun(car) = 2*car^3'. anything. So how do I make sure that I am replacing the car with the value.
Star Strider
Star Strider el 18 de Feb. de 2015
With ‘fun’ defined as:
fun = @(x) 2*x.^3;
the result for each of those would be:
result = fun(var_name)
result = fun(pos)
result = fun(car)
The function takes care of replacing the value appropriately. To understand how functions work, please see the documentation for ‘Anonymous Functions’ that I provided the hyperlink to in my Answer.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by