Identify Program Dependencies

If you need to know what other functions and scripts your program is dependent upon, use one of the techniques described below.

Simple Display of Program File Dependencies

For a simple display of all program files referenced by a particular function, follow these steps:

  1. Type clear functions to clear all functions from memory (see Note below).

    Note

    clear functions does not clear functions locked by mlock. If you have locked functions (which you can check using inmem) unlock them with munlock, and then repeat step 1.

  2. Execute the function you want to check. Note that the function arguments you choose to use in this step are important, because you can get different results when calling the same function with different arguments.

  3. Type inmem to display all program files that were used when the function ran. If you want to see what MEX-files were used as well, specify an additional output:

    [mfiles, mexfiles] = inmem

Detailed Display of Program File Dependencies

For a more detailed display of dependent function information, use the matlab.codetools.requiredFilesAndProducts function. In addition to program files, matlab.codetools.requiredFilesAndProducts shows which MathWorks® products a particular function depends on. If you have a function, myFun, that calls to the edge function in the Image Processing Toolbox™:

[fList,pList] = matlab.codetools.requiredFilesAndProducts('myFun.m');
fList
fList = 

    'C:\work\myFun.m'

The only required program file, is the function file itself, myFun.

{pList.Name}'
ans = 

    'MATLAB'
    'Image Processing Toolbox'

The file, myFun.m, requires both MATLAB® and the Image Processing Toolbox.

Dependencies Within a Folder

The Dependency Report shows dependencies among MATLAB code files in a folder. Use this report to determine:

  • Which files in the folder are required by other files in the folder

  • If any files in the current folder will fail if you delete a file

  • If any called files are missing from the current folder

The report does not list:

  • Files in the toolbox/matlab folder because every MATLAB user has those files.

    Therefore, if you use a function file that shadows a built-in function file, MATLAB excludes both files from the list.

  • Files called from anonymous functions.

  • The superclass for a class file.

  • Files called from eval, evalc, run, load, function handles, and callbacks.

    MATLAB does not resolve these files until run time, and therefore the Dependency Report cannot discover them.

  • Some method files.

    The Dependency Report finds class constructors that you call in a MATLAB file. However, any methods you execute on the resulting object are unknown to the report. These methods can exist in the classdef file, as separate method files, or files belonging to superclass or superclasses of a method file.

To provide meaningful results, the Dependency Report requires the following:

  • The search path when you run the report is the same as when you run the files in the folder. (That is, the current folder is at the top of the search path.)

  • The files in the folder for which you are running the report do not change the search path or otherwise manipulate it.

  • The files in the folder do not load variables, or otherwise create name clashes that result in different program elements with the same name.

Note

Do not use the Dependency Report to determine which MATLAB code files someone else needs to run a particular file. Instead use the matlab.codetools.requiredFilesAndProducts function.

Creating Dependency Reports

  1. Use the Current Folder pane to navigate to the folder containing the files for which you want to produce a Dependency Report.

    Note

    You cannot run reports when the path is a UNC (Universal Naming Convention) path; that is, a path that starts with \\. Instead, use an actual hard drive on your system, or a mapped network drive.

  2. On the Current Folder pane, click , and then select Reports > Dependency Report.

    The Dependency Report opens in the MATLAB Web Browser.

  3. If you want, select one or more options within the report, as follows:

    • To see a list of all MATLAB code files (children) called by each file in the folder (parent), select Show child functions.

      The report indicates where each child function resides, for example, in a specified toolbox. If the report specifies that the location of a child function is unknown, it can be because:

      • The child function is not on the search path.

      • The child function is not in the current folder.

      • The file was moved or deleted.

    • To list the files that call each MATLAB code file, select Show parent functions.

      The report limits the parent (calling) functions to functions in the current folder.

    • To include local functions in the report, select Show subfunctions. The report lists local functions directly after the main function and highlights them in gray.

  4. Click Run Report on Current Folder.

Reading and Working with Dependency Reports

The following image shows a Dependency Report. It indicates that chirpy.m calls two files in Signal Processing Toolbox™ and one in Image Processing Toolbox. It also shows that go.m calls mobius.m, which is in the current folder.

The Dependency Report includes the following:

  • MATLAB File List

    The list of files in the folder on which you ran the Dependency Report. Click a link in this column to open the file in the Editor.

  • Children

    The function or functions called by the MATLAB file.

    Click a link in this column to open the MATLAB file listed in the same row, and go to the first reference to the called function. For instance, suppose your Dependency Report appears as shown in the previous image. Clicking \images\images\erode.m opens chirpy.m and places the cursor at the first line that references erode. In other words, it does not open erode.m.

  • Multiple class methods

    Because the report is a static analysis, it cannot determine run-time data types and, therefore, cannot identify the particular class methods required by a file. If multiple class methods match a referenced method, the Dependency Report inserts a question mark link next to the file name. The question mark appears in the following image.

    Click the question mark link to list the class methods with the specified name that MATLAB might use. MATLAB lists almost all the method files on the search path that match the specified method file (in this case, freqresp.m). Do not be concerned if the list includes methods of classes and MATLAB built-in functions that are unfamiliar to you.

    It is not necessary for you to determine which file MATLAB will use. MATLAB determines which method to use depending on the object that the program calls at run time.