Main Content

str2sym

Evaluate string representing symbolic expression

Description

example

str2sym(symstr) evaluates symstr where symstr is a string representing a symbolic expression. Enter symbolic expressions as strings only when reading expressions from text files or when specifying numbers exactly. Otherwise, do not use strings for symbolic input.

Examples

collapse all

Evaluate the string 'sin(pi)'. str2sym returns the expected result.

str2sym('sin(pi)')
ans =
0

str2sym assumes the = operator represents an equation, not an assignment. Also, str2sym does not add the variables contained in the string to the workspace.

Show this behavior by evaluating 'x^2 = 4'. The str2sym function returns the equation x^2 == 4 but x does not appear in the workspace.

eqn = str2sym('x^2 = 4')
eqn =
x^2 == 4

Find the variable in eqn by using symvar. The variable var now refers to x.

var = symvar(eqn)
var =
x

Assign values from eqn by solving eqn for var and assigning the result.

varVal = solve(eqn,var)
varVal =
 -2
  2

str2sym does not substitute values from the workspace for variables in the input. Therefore, str2sym has reproducible output. Instead, substitute workspace values by using subs on the output of str2sym.

Set y to 2. Then, evaluate 'y^2' with and without subs to show how subs substitutes y with its value.

y = 2;
withoutSubs = str2sym('y^2')
withoutSubs =
y^2
withSubs = subs(str2sym('y^2'))
withSubs =
4

When symbolic expressions are stored as strings in a file, evaluate the strings by reading the file and using str2sym.

Assume the file mySym.txt contains this text.

a = 2.431
y = a*exp(t)
diff(z(t),t) = b*y*z

Evaluate expressions in mySym.txt using str2sym.

filename = 'mySym.txt';
filetext = fileread(filename);
filetext = splitlines(filetext);
str2sym(filetext)
ans =
           a == 2.431
        y == a*exp(t)
 diff(z(t), t) == b*y*z

The output of str2sym is independent of workspace values, which means the output is reproducible. Show this reproducibility by assigning a value to b and re-evaluating the stored expressions.

b = 5;
str2sym(filetext)
ans =
           a == 2.431
        y == a*exp(t)
 diff(z(t), t) == b*y*z

To use workspace values or a value from input equations, use subs (solve the equation first using solve), as described in Evaluate String as Symbolic Expression and Substitute Workspace Values into String Input.

str2sym executes functions in input when the functions are on the path. Otherwise, str2sym returns the symbolic object as expected. This behavior means that the output is reproducible.

Show this behavior by reading a differential equation and initial condition from a file. Solve the equation for the condition. Because str2sym does not evaluate y(t) in the equation, the output is reproducible.

filename = 'mySym.txt';
filetext = fileread(filename);
filetext = splitlines(filetext);
eqn = str2sym(filetext(1))
eqn =
diff(y(t), t) == -y(t)
cond = str2sym(filetext(2))
cond =
y(0) == 2
ySol = dsolve(eqn,cond)
ySol =
2*exp(-t)

Because the MATLAB® parser automatically converts all numbers to double precision, maintain original precision by entering large numbers and high-precision numbers as strings. Instead of str2sym, enter integers using sym and floating-point numbers using vpa because sym and vpa are faster.

Show the error between entering a ratio of large integers directly versus the exact string representation.

num = sym(12230984290/38490293482)
num =
5724399718238385/18014398509481984
numExact = sym('12230984290/38490293482')
numExact =
6115492145/19245146741
error = num - numExact
error =
-7827162395/346689742765832461975814144

Show the error between entering a high-precision number directly versus the exact string representation.

num = vpa(8.023098429038490293482)
num =
8.0230984290384910195825796108693
numExact = vpa('8.023098429038490293482')
numExact =
8.023098429038490293482
error = num - numExact
error =
0.00000000000000072610057961086928844451883343504

For details, see Numeric to Symbolic Conversion. For full workflows, see Find Almost Integers with High-Precision Arithmetic and Prime Factorizations.

Starting in R2019b, you can represent hexadecimal and binary values using character vectors. Hexadecimal values start with a 0x or 0X prefix, while binary values start with a 0b or 0B prefix. You can then convert the hexadecimal and binary values to symbolic decimal numbers using str2sym. For more information, see Hexadecimal and Binary Values.

Create a character vector that represents a hexadecimal value. Convert the value to symbolic decimal number.

H = '0x2A'
D = str2sym(H)
D =
42

Create a character vector that represents a binary value. Convert the value to symbolic decimal number.

B = '0b101010'
D = str2sym(B)
D =
42

Input Arguments

collapse all

String representing a symbolic expression, specified as a character vector, string, or cell array of character vectors.

Tips

  • str2sym assumes the = operator represents an equation, not an assignment.

  • str2sym does not create variables contained in the input.

  • str2sym('inf') returns infinity (Inf).

  • str2sym('i') returns the imaginary number 1i.

Version History

Introduced in R2017b