필터 지우기
필터 지우기

How to replace the string using strrep?

조회 수: 2 (최근 30일)
Kratos
Kratos 2015년 2월 18일
댓글: Star Strider 2015년 2월 18일
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?

채택된 답변

Star Strider
Star Strider 2015년 2월 18일
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
Kratos
Kratos 2015년 2월 18일
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 2015년 2월 18일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by