str2sym

Converts a string of MATLAB syntax to a symbolic expression.

Category: Symbolic Computations in MATLAB > Symbolic Variables, Expressions, Functions, and Preferences

Syntax

S = str2sym(text)

Parses text and returns the symbolic expression it describes.

Description

Names in the text become symbolic variables, and numbers are read exactly, so '1/3' stays the rational 1/3 instead of a rounded double. The text is parsed, not evaluated as MATLAB code, so it cannot call arbitrary functions with side effects. Invalid text raises a parse error.

Unlike sym with a string, str2sym is the recommended way to turn text from a file or a user into an expression, and it understands relations such as ==.

Input arguments

text
String or character vector holding an expression, an equation with ==, or a matrix such as '[1 2; 3 4]'.

Output arguments

S
Symbolic expression, equation or matrix.

Example

Expressions, matrices and equations

f = str2sym('x^2 + 3*x')
M = str2sym('[1 2; 3 4]')
e = str2sym('x^2 - y == 3')
class(f)
Output
f = x^2 + 3*x
M = 
[1, 2]
[3, 4]
e = x^2 - y == 3
ans = 'sym'

See also

sym syms eval char symvar