2

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?

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
craigim
  • 3,884
  • 1
  • 22
  • 42

2 Answers2

3

%#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.

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
  • So `pragma` was the word I was looking for, and of course %#function is the heading of the page that shows up when you search for "pragma" in the documention. The dependency checker also uses the pragma, which was how I rediscovered it as a vestigial bit of code that was inserting a depreciated function into the dependency list. Any thoughts on the second part of the question about how to force the dependency checker to include non-m-files? – craigim Oct 02 '14 at 18:16
  • There are a few other questions here on SO regarding MATLAB function [dependencies](http://stackoverflow.com/questions/tagged/matlab+dependencies) and [code-analysis](http://stackoverflow.com/questions/tagged/matlab+code-analysis), with possible solutions to your problem. – Amro Oct 02 '14 at 18:19
  • I've read most of them before. I don't remember seeing anything about the auxiliary files, but I'll look again. – craigim Oct 02 '14 at 18:28
  • I don't think this is a completely solved problem.. For instance when you package an App using MathWorks provided tool, you have to manually specify auxiliary files that are not found via dependency analysis: http://www.mathworks.com/help/matlab/creating_guis/app-creation.html – Amro Oct 02 '14 at 18:38
  • I [once came across](http://stackoverflow.com/q/20669286/97160) a set of [projects](http://www.sable.mcgill.ca/mclab/projects/) that [provide](https://github.com/Sable/mclab) parsers and code analysis tools for the MATLAB language, that could potentially be used for deep dependency analysis like you're looking for. Check it out if you want :) – Amro Oct 02 '14 at 18:48
  • I do not live in a programmer world, so any tools that I use need to only live in MATLAB, but I think that I've cobbled together something that works reasonably well. I also submitted a feature request to Mathworks to make their dependency tools smarter. We'll see what happens. – craigim Oct 03 '14 at 18:22
0

I don't have any answer, but maybe you can use this website: http://www.symbolhound.com/

It let you do search using symbols.

  • Good to know, but alas, no dice. The search yielded 4 stackoverflow questions, only two of which actually had that the string `%#function` anywhere, neither of which had anything to say about the syntax. – craigim Oct 02 '14 at 17:45
  • 1
    @craigim: careful that many people (myself included) use the `%#` or `%//` symbols here on Stack Overflow to get comments properly highlighted (we are [still waiting](http://meta.stackexchange.com/a/137865/135945) for MATLAB code to be correctly rendered on SO). This has nothing to do with the actual pragma though! – Amro Oct 02 '14 at 18:09