converting sym function to string MATLAB

I am attempting to convert a symbolic expression to a string using the function sym2str but I am getting an error. How can I convert a symbolic expression to a string? See attachment below

 Respuesta aceptada

Try this —
syms x y
eqn = x^2+2*x*y+3*y^2;
partial_eqn = diff(eqn, x);
fprintf("The partial derivative of %s w.r.t. x is %s \n", string(eqn), string(partial_eqn))
The partial derivative of 2*x*y + x^2 + 3*y^2 w.r.t. x is 2*x + 2*y
Also, using fprintf is easier and creates a more pleasing result.
.

5 comentarios

Andromeda
Andromeda el 17 de Oct. de 2021
Thank you, that helped.
Star Strider
Star Strider el 17 de Oct. de 2021
As always, my pleasure!
.
Jagan
Jagan el 22 de Mayo de 2023
i have one question , how i can convert "sym" class to string? i tried all these but not successfully it.
Star Strider
Star Strider el 22 de Mayo de 2023
@Jagan — I suppose that it depends on what you are converting. This approach works here.
If you have a specific problem, it would be best for you to post it as a new Question, along with the sym expression you want to convert.
Walter Roberson
Walter Roberson el 22 de Mayo de 2023
string() should give you the same printable representation that displaying at the command line gives.
Note however, that the printable representation is not necessarily something that can be executed as MATLAB code or as symbolic code or as the internal MuPAD symbolic language. The printable display of symbolic functions and symbolic expressions is intended for human understanding, not for machine execution.
If you are looking for something that has to be machine executable then use matlabFunction after which you can use func2str

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 17 de Oct. de 2021

Comentada:

el 22 de Mayo de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by