Convert a symbolic expression to string

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

Réponses (3)

Walter Roberson
Walter Roberson le 23 Nov 2018

0 votes

As you appear to want to use string objects use compose()

2 commentaires

Since R2019b, you can do
A=9
A = 9
B=5
B = 5
displayFormula('A+B-(A*B)')
Notice that the () disappeared though.
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

Connectez-vous pour commenter.

Rajan Prasad
Rajan Prasad le 7 Août 2022

0 votes

Use string() command. This will convert the symbolic expression into string and then you can export into any format.

2 commentaires

Walter Roberson
Walter Roberson le 7 Août 2022
That will not work to prevent evaluation of the expression while still substituting in values of the variables.
Rajan Prasad
Rajan Prasad le 7 Août 2022
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Produits

Version

R2018a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by