Documentation

This is machine translation

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

which

Locate functions and files

Syntax

Description

example

which item displays the full path for item.

  • If item is a MATLAB® function in a MATLAB code file (.m,.mlx, or .p extension), or a Simulink® model (.slx or .mdl extension), then which displays the full path for the corresponding file. item must be on the MATLAB path.

  • If item is a method in a loaded Java® class, then which displays the package, class, and method name for that method.

  • If item is a workspace variable, then which displays a message identifying item as a variable.

  • If item is a file name including the extension, and it is in the current working folder or on the MATLAB path, then which displays the full path of item.

If item is an overloaded function or method, then which item returns only the path of the first function or method found.

example

which fun1 in fun2 displays the path to function fun1 that is called by file fun2. Use this syntax to determine whether a local function is being called instead of a function on the path. This syntax does not locate nested functions.

example

which ___ -all displays the paths to all items on the MATLAB path with the requested name. Such items include methods of instantiated classes. You can use -all with the input arguments of any of the previous syntaxes.

example

str = which(item) returns the full path for item to str.

example

str = which(fun1,'in',fun2) returns the path to function fun1 that is called by file fun2. Use this syntax to determine whether a local function is being called instead of a function on the path. This syntax does not locate nested functions.

example

str = which(___,'-all') returns the results of which to str. You can use this syntax with any of the input arguments in the previous syntax group.

Examples

collapse all

Locate the pinv function.

which pinv
matlabroot\toolbox\matlab\matfun\pinv.m

pinv is in the matfun folder of MATLAB.

You also can use function syntax to return the path to str. When using the function form of which, enclose all input arguments in single quotes.

str = which('pinv');

Create an instance of the Java® class. This loads the class into MATLAB®.

myDate = java.util.Date;

Locate the setMonth method.

which setMonth
setMonth is a Java method  % java.util.Date method

Find the orthog function in a private folder.

which private/orthog
matlabroot\toolbox\matlab\elmat\private\orthog.m  % Private to elmat

MATLAB displays the path for orthog.m in the /private subfolder of toolbox/matlab/elmat.

Determine which parseargs function is called by area.m.

which parseargs in area
% Local function of area
matlabroot\toolbox\matlab\specgraph\area.m (parseargs)  

You also can use function syntax to return the path to str. When using the function form of which, enclose all input arguments in single quotes.

str = which('parseargs','in','area');

Suppose you have a matlab.io.MatFile object that corresponds to the example MAT-file 'topography.mat':

matObj = matfile('topography.mat');

Display the path of the implementation of who that is invoked when called with the input argument (matObj).

which who(matObj)
% matlab.io.MatFile method
matlabroot\toolbox\matlab\iofun\+matlab\+io\MatFile.m  

Store the result to the variable str.

str = which('who(matObj)')
str =
matlabroot\toolbox\matlab\iofun\+matlab\+io\MatFile.m

If you do not specify the input argument (matObj), then which returns only the path of the first function or method found.

which who
built-in (matlabroot\\toolbox\matlab\general\who)

Display the paths to all items on the MATLAB path with the name fopen.

which fopen -all
built-in (matlabroot\toolbox\matlab\iofun\fopen)
% serial method
matlabroot\toolbox\matlab\iofun\@serial\fopen.m

% icinterface method
matlabroot\toolbox\shared\instrument\@icinterface\fopen.m  
matlabroot\toolbox\instrument\instrument\@i2c\fopen.m      

Return the results of which to str.

Find the orthog function in a private folder. You must use the function form of which, enclosing all arguments in parentheses and single quotes.

str = which('private/orthog','-all');
whos str
  Name      Size            Bytes  Class    Attributes

  str       1x1               270  cell               

Input Arguments

collapse all

Function or file to locate, specified as a character vector. When using the function form of which, enclose all item inputs in single quotes. item can be in one of the following forms.

Form of the item InputPath to Display
fun

Display full path for fun, which can be a MATLAB function, Simulink model, workspace variable, method in a loaded Java class, or file name that includes the file extension.

To display the path for a file that has no file extension, type which file. (The period following the filename is required). Use exist to check for the existence of files anywhere else.

/fun

Limit the search to functions named fun that are on the search path. For example, which /myfunction displays the full path for function myfunction.m, but not built-in or JAVA functions with the same name.

private/funLimit the search to private functions named fun. For example, which private/orthog or which('private/orthog') displays the path for orthog.m in the /private subfolder of the parent folder.

fun(a1,...,an)

Display the path to the implementation of function fun which would be invoked if called with the input arguments a1,...,an. Use this syntax to query overloaded functions. See the Example, Locate Function Invoked with Given Input Arguments.

Function to locate, specified as a character vector. fun1 can be the name of a function, or it can be in the form fun(a1,...,an). For more information about the form, fun(a1,...,an), see Locate Function Invoked with Given Input Arguments.

When using the function form of which, enclose all fun1 inputs in single quotes, for example, which('myfun1','in','myfun2').

Calling file, specified as a character vector. fun2 can be the name of a file, or it can be in the form fun(a1,...,an). For more information about the form, fun(a1,...,an), see Locate Function Invoked with Given Input Arguments.

When using the function form of which, enclose all fun2 inputs in single quotes, for example, which('myfun1','in','myfun2').

Output Arguments

collapse all

Function or file location, returned as a character vector or cell array of character vectors if you use '-all'.

  • If item is a workspace variable, then str is the character vector 'variable'.

  • If str is a cell array of character vectors, then each row of str identifies a function. The functions are in order of precedence, unless they are shadowed. Among shadowed functions, you should not rely on the order of the functions in str. To determine if a function is shadowed, call which using command syntax. For example, which test -all displays the paths to all functions with the name test. Shadowed functions are indicated by the comment, % Shadowed.

Limitations

  • When the class is not loaded, which only finds methods if they are defined in separate files in an @-folder and are not in any packages.

More About

collapse all

Tips

See Also

| | | | | | | | |

Introduced before R2006a

Was this topic helpful?