which - Locate functions and files - MATLAB (original) (raw)

Locate functions and files

Syntax

Description

which [item](#bt07pwb) displays the full path foritem.

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](#btmmf7i-1-fun1) in [fun2](#btmmf7i-1-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, as well as any files in special folders that have been implicitly added to the path. Such items include methods of instantiated classes. For more information about these special folders, seeWhat Is the MATLAB Search Path. You can use -all with the input arguments of any of the previous syntaxes.

example

[str](#btmmf7i-1-str) = which([item](#bt07pwb)) returns the full path for item to str.

example

[str](#btmmf7i-1-str) = which([fun1](#btmmf7i-1-fun1),'in',[fun2](#btmmf7i-1-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](#btmmf7i-1-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.

example

Examples

collapse all

Locate MATLAB Function

Locate the pinv function.

_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.

Locate Method in a Loaded Java Class

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

Locate the setMonth method.

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

Locate Private Function

Find the orthog function in a private folder.

_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 If Local Function Is Called

Determine which parseargs function is called by area.m.

_matlabroot_\toolbox\matlab\specgraph\area.m (parseargs) % Local function of area

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');

Locate Function Invoked with Given Input Arguments

Suppose that 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).

_matlabroot_\toolbox\matlab\iofun+matlab+io\MatFile.m % matlab.io.MatFile method

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.

built-in (_matlabroot_\toolbox\matlab\general\who)

Locate All Items with Given Name

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

built-in (matlabroot\toolbox\matlab\iofun\openedFiles)

Return Path Names

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 348 cell

Input Arguments

collapse all

item — Function or file to locate

character vector | string scalar

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

Form of the item Input Path 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 file name is required). Useexist 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/fun Limit 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 argumentsa1,...,an. Use this syntax to query overloaded functions. See the example, Locate Function Invoked with Given Input Arguments.

Data Types: char | string

fun1 — Function to locate

character vector | string scalar

Function to locate, specified as a character vector or string scalar. fun1 can be the name of a function, or it can be in the formfun(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 allfun1 inputs in single or double quotes, for example,which('myfun1','in','myfun2').

Data Types: char | string

fun2 — Calling file

character vector | string scalar

Calling file, specified as a character vector or string scalar. fun2 can be the name of a file, or it can be in the formfun(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 allfun2 inputs in single or double quotes, for example,which('myfun1','in','myfun2').

Data Types: char | string

Output Arguments

collapse all

str — Function or file location

character vector | cell array of character vectors

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

Limitations

Tips

Extended Capabilities

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

Version History

Introduced before R2006a