How to replace the string using strrep?

2 views (last 30 days)
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?

Accepted Answer

Star Strider
Star Strider on 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 Comments
Kratos
Kratos on 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 on 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!