Filter löschen
Filter löschen

How to replace the string using strrep?

2 Ansichten (letzte 30 Tage)
Kratos
Kratos am 18 Feb. 2015
Kommentiert: Star Strider am 18 Feb. 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?

Akzeptierte Antwort

Star Strider
Star Strider am 18 Feb. 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 Kommentare
Kratos
Kratos am 18 Feb. 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 am 18 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by