1########################################################################
2##
3## Copyright (C) 2008-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn {} {@var{vars} =} symvar (@var{str})
28## Identify the symbolic variable names in the string @var{str}.
29##
30## Common constant names such as @code{i}, @code{j}, @code{pi}, @code{Inf} and
31## Octave functions such as @code{sin} or @code{plot} are ignored.
32##
33## Any names identified are returned in a cell array of strings.  The array is
34## empty if no variables were found.
35##
36## Example:
37##
38## @example
39## @group
40## symvar ("x^2 + y^2 == 4")
41## @result{} @{
42##      [1,1] = x
43##      [2,1] = y
44##    @}
45## @end group
46## @end example
47## @end deftypefn
48
49function vars = symvar (str)
50
51  vars = argnames (inline (str));
52  ## Correct for auto-generated 'x' variable when no symvar was found.
53  if (numel (vars) == 1 && strcmp (vars{1}, "x") && ! any (str == "x"))
54    vars = {};
55  endif
56
57endfunction
58
59
60%!assert (symvar ("3*x + 4*y + 5*cos (z)"), {"x"; "y"; "z"})
61%!assert (symvar ("sin()^2 + cos()^2 == 1"), {})
62%!assert (symvar ("1./x"), {"x"})
63