which

Locates a function, script, file or variable.

Category: Programming > Files and Folders

Syntax

which name

Displays where name is defined.

p = which(name)

Returns the path of the file, or text describing a built-in or variable.

which name -all

Lists every definition that exists, including shadowed ones.

Description

which follows the same lookup rules as a call, so it shows what a name will really run. It is the first tool to use when a function behaves unexpectedly because a different file of the same name sits earlier on the path. For a workspace variable it says that the name is a variable.

Input arguments

name
Function, script, class or file name, or a variable.

Output arguments

p
Path of the file that would run for name; empty if nothing is found.

Example

Finding a function file and a variable

mkdir lib
writelines('function y = triple(x), y = 3*x; end', 'lib/triple.m');
addpath('lib')
which triple
x = 5;
which x
Output
lib\triple.m
x is a variable.

Notes

For functions implemented inside MatLivre, which reports built-in (MatLivre) even where MATLAB would show an M-file path.

See also

exist what addpath path dir