Convert a symbolic expression to string

조회 수: 145 (최근 30일)
Saikat Banerjee
Saikat Banerjee 2018년 11월 23일
댓글: Rajan Prasad 2022년 8월 7일
I haVE A SYMBOLIC EXPRESSION WHICHI WANT TO USE AS A STRING EXPRESSION WITHOUT EVALUATING IT. EXAMPLE IF I HAVE
A=9
B=5
IF WE WRITE STRING(A+B-(A*B) ) WE GET ANSWER -31
BUT I WANT 9+5-(9*5) . HOW TO DO IT

답변 (3개)

madhan ravi
madhan ravi 2018년 11월 23일
편집: madhan ravi 2018년 11월 23일

Walter Roberson
Walter Roberson 2018년 11월 23일
As you appear to want to use string objects use compose()
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 8월 7일
Since R2019b, you can do
A=9
A = 9
B=5
B = 5
displayFormula('A+B-(A*B)')
Notice that the () disappeared though.
Walter Roberson
Walter Roberson 2022년 8월 7일
A = 9; B = 5;
str = 'A+B-(A*B)/GRAB'
str = 'A+B-(A*B)/GRAB'
regexprep(str, {'\<A\>', '\<B\>'}, {num2str(A), num2str(B)})
ans = '9+5-(9*5)/GRAB'
The \< and \> are there to force it to replace only when the names are the entire "word" -- the A and B inside of GRAB should not be replaced

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


Rajan Prasad
Rajan Prasad 2022년 8월 7일
Use string() command. This will convert the symbolic expression into string and then you can export into any format.
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 8월 7일
That will not work to prevent evaluation of the expression while still substituting in values of the variables.
Rajan Prasad
Rajan Prasad 2022년 8월 7일
The string command will convert the symbolic expression into string. Instead of typing/entering the symbolic expression agian, one can use string. Then can use regexprep as suggested by you to replace the desired string to get in the string format with value.

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

카테고리

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

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by