Mercurial > hg > octave-nkf
comparison scripts/help/which.m @ 18519:2bd78ab75b23
which: Properly identify workspace variables (bug #37320)
* which.m: Check whether each name is a variable, for compatibility with
Matlab and consistency with exist(). Add %!tests for new behavior.
author | Mike Miller <mtmiller@ieee.org> |
---|---|
date | Sat, 08 Feb 2014 14:39:33 -0500 |
parents | d63878346099 |
children | 4197fc428c7d |
comparison
equal
deleted
inserted
replaced
18517:0cbd0d285541 | 18519:2bd78ab75b23 |
---|---|
26 function varargout = which (varargin) | 26 function varargout = which (varargin) |
27 | 27 |
28 if (nargin > 0 && iscellstr (varargin)) | 28 if (nargin > 0 && iscellstr (varargin)) |
29 m = __which__ (varargin{:}); | 29 m = __which__ (varargin{:}); |
30 | 30 |
31 ## Check whether each name is a variable, variables take precedence over | |
32 ## functions in name resolution. | |
33 for i = 1:nargin | |
34 m(i).is_variable = evalin ("caller", | |
35 ["exist (\"" m(i).name "\", \"var\")"], false); | |
36 if (m(i).is_variable) | |
37 m(i).file = "variable"; | |
38 endif | |
39 endfor | |
40 | |
31 if (nargout == 0) | 41 if (nargout == 0) |
32 for i = 1:nargin | 42 for i = 1:nargin |
33 if (isempty (m(i).file)) | 43 if (m(i).is_variable) |
44 printf ("'%s' is a variable\n", m(i).name); | |
45 elseif (isempty (m(i).file)) | |
34 if (! isempty (m(i).type)) | 46 if (! isempty (m(i).type)) |
35 printf ("'%s' is a %s\n", | 47 printf ("'%s' is a %s\n", |
36 m(i).name, m(i).type); | 48 m(i).name, m(i).type); |
37 endif | 49 endif |
38 else | 50 else |
62 %! str = which ("amd"); | 74 %! str = which ("amd"); |
63 %! assert (str(end-6:end), "amd.oct"); | 75 %! assert (str(end-6:end), "amd.oct"); |
64 | 76 |
65 %!assert (which ("_NO_SUCH_NAME_"), "") | 77 %!assert (which ("_NO_SUCH_NAME_"), "") |
66 | 78 |
79 %!test | |
80 %! x = 3; | |
81 %! str = which ("x"); | |
82 %! assert (str, "variable"); | |
83 | |
84 %!test | |
85 %! str = which ("amd"); | |
86 %! assert (str(end-6:end), "amd.oct"); | |
87 %! amd = 12; | |
88 %! str = which ("amd"); | |
89 %! assert (str, "variable"); | |
90 %! clear amd; | |
91 %! str = which ("amd"); | |
92 %! assert (str(end-6:end), "amd.oct"); |