Effacer les filtres
Effacer les filtres

How to replace the string using strrep?

2 vues (au cours des 30 derniers jours)
Kratos
Kratos le 18 Fév 2015
Commenté : Star Strider le 18 Fév 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?

Réponse acceptée

Star Strider
Star Strider le 18 Fév 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 commentaires
Kratos
Kratos le 18 Fév 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 le 18 Fév 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by