Find symbolic variables in symbolic expression, matrix, or function
symvar(s) returns a vector containing all the symbolic variables in s in alphabetical order with uppercase letters preceding lowercase letters.
symvar(s,n) returns a vector containing n symbolic variables in s alphabetically closest to x. If s is a symbolic function, symvar(s,n) returns the input arguments of s in front of other free variables in s.
Find all symbolic variables in the sum:
syms wa wb wx yx ya yb symvar(wa + wb + wx + ya + yb + yx)
ans = [ wa, wb, wx, ya, yb, yx]
Find all symbolic variables in this function:
syms x y a b f(a, b) = a*x^2/(sin(3*y - b)); symvar(f)
ans = [ a, b, x, y]
Now find the first three symbolic variables in f. For a symbolic function, symvar with two arguments returns the function inputs in front of other variables:
ans = [ a, b, x]
For a symbolic expression or matrix, symvar with two arguments returns variables sorted by their proximity to x:
symvar(a*x^2/(sin(3*y - b)), 3)
ans = [ x, y, b]
Find the default symbolic variable of these expressions:
syms v z g = v + z; symvar(g, 1)
ans = z
syms aaa aab g = aaa + aab; symvar(g, 1)
ans = aaa
syms X1 x2 xa xb g = X1 + x2 + xa + xb; symvar(g, 1)
ans = x2
symvar(s) can return variables in a different order than symvar(s,n).
symvar does treat the constants pi, i, and j as variables.
If there are no symbolic variables in s, symvar returns the empty vector.
When performing differentiation, integration, substitution or solving equations, MATLAB® uses the variable returned by symvar(s,1) as a default variable. For a symbolic expression or matrix, symvar(s,1) returns the variable closest to x. For a function, symvar(s,1) returns the first input argument of s.
When sorting the symbolic variables by their proximity to x, symvar uses this algorithm:
The variables are sorted by the first letter in their names. The ordering is x y w z v u ... a X Y W Z V U ... A. The name of a symbolic variable cannot begin with a number.
For all subsequent letters, the ordering is alphabetical, with all uppercase letters having precedence over lowercase: 0 1 ... 9 A B ... Z a b ... z.