# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

# symvar

Find symbolic variables in symbolic expression, matrix, or function

## Syntax

```symvar(s) symvar(s,n) ```

## Description

`symvar(s)` returns a vector containing all the symbolic variables in `s` in alphabetical order with uppercase letters preceding lowercase letters.

`symvar(s,n)` chooses the `n` symbolic variables in `s` that are alphabetically closest to `x` and sorts them alphabetically before returning them. If `s` is a symbolic function, `symvar(s,n)` returns the input arguments of `s` before other free variables in `s`.

## Input Arguments

 `s` Symbolic expression, matrix, or function. `n` Integer or `Inf`. If `n` exceeds the number of variables in `s`, then `symvar(s,n)` is equivalent to `symvar(s,m)` where `m` is the number of variables in `s`.

## Examples

Find all symbolic variables in this 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]```

Find the first three symbolic variables in `f`. For a symbolic function, `symvar` with two arguments returns the function inputs before other variables.

`symvar(f, 3)`
```ans = [ a, b, x]```

Find the first three symbolic variables in an expression. For a symbolic expression or matrix, `symvar` chooses variables alphabetically closest to `x` and sorts them alphabetically.

`symvar(a*x^2/(sin(3*y - b)), 3)`
```ans = [ b, x, y]```

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```

## Tips

• `symvar` treats the constants `pi`, `i`, and `j` as variables.

• If there are no symbolic variables in `s`, `symvar` returns the empty vector.

• When differentiating, integrating, substituting, 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`.

## Algorithms

When `symvar` sorts the symbolic variables alphabetically, all uppercase letters have precedence over lowercase: 0 1 ... 9 A B ... Z a b ... z.