comparison scripts/miscellaneous/symvar.m @ 19240:65cf441abc5e

symvar.m: Correct the return value when no arguments were found. * symvar.m: Redo docstring. Return an empty cell array if no variables were found for Matlab compatibility. Add BIST tests.
author Rik <rik@octave.org>
date Fri, 03 Oct 2014 17:16:59 -0700
parents d63878346099
children db92e7e28e1f
comparison
equal deleted inserted replaced
19239:e172186599ca 19240:65cf441abc5e
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} symvar (@var{s}) 20 ## @deftypefn {Function File} {@var{vars} =} symvar (@var{str})
21 ## Identify the argument names in the function defined by a string. 21 ## Identify the symbolic variable names in the string @var{str}.
22 ## Common constant names such as @code{pi}, @code{NaN}, @code{Inf}, 22 ##
23 ## @code{eps}, @code{i} or @code{j} are ignored. The arguments that are 23 ## Common constant names such as @code{i}, @code{j}, @code{pi}, @code{Inf} and
24 ## found are returned in a cell array of strings. If no variables are 24 ## Octave functions such as @code{sin} or @code{plot} are ignored.
25 ## found then the returned cell array is empty. 25 ##
26 ## Any names identified are returned in a cell array of strings. The array is
27 ## empty if no variables were found.
28 ##
29 ## Example:
30 ##
31 ## @example
32 ## @group
33 ## symvar ("x^2 + y^2 == 4")
34 ## @result{} @{
35 ## [1,1] = x
36 ## [2,1] = y
37 ## @}
38 ## @end group
39 ## @end example
26 ## @end deftypefn 40 ## @end deftypefn
27 41
28 function args = symvar (s) 42 function vars = symvar (str)
29 args = argnames (inline (s)); 43 vars = argnames (inline (str));
44 ## Correct for auto-generated 'x' variable when no symvar was found.
45 if (numel (vars) == 1 && strcmp (vars{1}, "x") && ! any (str == "x"))
46 vars = {};
47 endif
48
30 endfunction 49 endfunction
31 50
32 51
33 ## This function is tested by the tests for argnames(). 52 %!assert (symvar ("3*x + 4*y + 5*cos (z)"), {"x"; "y"; "z"})
34 %!assert (1) 53 %!assert (symvar ("sin()^2 + cos()^2 == 1"), {})
54 %!assert (symvar ("1./x"), {"x"})
35 55