Search code examples
matlabdependency-managementpragma

Use of %# notation for declaring dependencies


In MATLAB, you can declare a function dependency with:

%#function myExtraFunctionName

Doing so tells MATLAB that myExtraFunctionName is required by the script or function to operate, even if it's called by an eval statement or some other method that the various dependency checkers or compilers can't figure out.

I have several files that load in .mat or other data files that are required for the script to run, and I would like to include them in a similar manner so that when I run a dependency check with, say fList = matlab.codetools.requiredFilesAndProducts, it will find these data files as well. Ultimately what I would like to be able to do is generate the list of files and pass it to zip to archive every file required to run a given script or function, including data files.

Trying to find any documentation on this feature is challenging because the MATLAB help won't let you just type in %# and searching for %#function just searches for function. Google does the same thing: "hash percent function" returns lots of information on hash tables, "%#function matlab" strips out the important characters, and "declare matlab function" "declare matlab function dependency" turns up nothing useful. I don't remember where I encountered this syntax, so I don't even know if this is a documented feature or not.

I have two questions:

  • Can someone point me to documentation on this syntax along with some clues as to what keywords I should be using to search?

  • Can this be used to declare dependencies other than m-files and, if not, how can I go about doing that?


Solution

  • %#function is a pragma directive that informs MATLAB Compiler that the specified function will be called indirectly using feval, eval, or the like.

    This is important because the static code analyzer will not be able to detect such dependencies on its own. For instance the name of the function could be stored in a string as in:

    fcn = 'myFunction';
    feval(fcn)
    

    As far as I know, this is only used by the MATLAB Compiler, nothing else.

    There are other similar pragmas. For example MATLAB Coder has %#codegen compiler directive.