Use of %# notation for declaring dependencies - matlab

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?

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

I don't have any answer, but maybe you can use this website:
http://www.symbolhound.com/
It let you do search using symbols.

Related

How to locate where a built-in function is defined?

In MATLAB, there are roughly 3 ways to define functions: non-comment-only .m files, .p files, and compiled code (e.g. DLL, MEX).
Knowing where a function is defined could be helpful in several cases, such as when a breaking change was introduced to some function outside our control, and we'd like to try to revert to an old version in the hopes of getting our code working again; or when trying to reverse-engineering some undisclosed algorithm.
The which function is usually very good at identifying function definitions and their locations (which works for .m, .p and MEX), but isn't very useful when it comes to shared library functions, where (at best) it points to a comment-only documentation file:
>> which _mcheck
built-in (undocumented)
>> which svd
built-in (D:\Program Files\MATLAB\R2019a\toolbox\matlab\matfun\svd)
If so, assuming a function found within a shared library is called during the execution of my code, how can I locate the specific file (DLL) that contains it?
It turns out that dbstop can be used for this. For example:
>> which svd
built-in (D:\Program Files\MATLAB\R2019a\toolbox\matlab\matfun\svd)
>> dbstop svd
Warning: Entering debug mode is only supported within running MATLAB code files.
Warning: MATLAB debugger can only stop in MATLAB code files, and "libmwmathlinalg>svd" is not a MATLAB code file.
Instead, the debugger will stop at the point right before "libmwmathlinalg>svd" is called.
From there's it's just a matter of finding a file called libmwmathlinalg (with the relevant extension) - which isn't a difficult task if your drive is indexed.

How to automatically convert a Matlab script into a Matlab function?

My problem is the following:
I have very many (~1000) mutually calling Matlab scripts, which are very poorly written, regularly damage each other's environments and generally became unmanageable.
One of the reasons I even got this problem is that I need to write a testsuite covering a big part of them. Luckily, for most of them the main criterion of 'correctness' is 'they don't crash'.
Just running them one by one in a loop is generally not an option, because they regularly call clear classes, close all, clc, shadow built-in functions and operators, et cetera.
So my original aim was to find a way to run a matlab script in sort of an 'isolated environment', but I didn't find a good way to do it. (Suggestions welcome, but it is not the main question.)
Since I will need to convert them all to functions anyway, I am looking for some way to do it auto-magically, or at least semi-automatically.
What I can mean semi-automatically:
Just add a line function varargout = $filename( varargin ) as the first line of the file, and end as the last one. This will at least make them runnable as functions with feval and all such functions and (more importantly) prevent them from damaging the test-runner.
Do point 1 and scan the file for referencing undeclared variables and add them as function arguments. This should be also doable, since the names of the variables are known. This will not help identifying output variables, but will still be a lot of help. For example, we could pack the whole workspace into one big output structure.
Do a runtime version of point 2. This way the 'magical converter' can actually track execution environments (workspaces) and identify which variables are implicitly used as 'input arguments' of a script, and which would be later used 'output arguments'. This option looks EXPHARD, but for a small number of calls should be not too bad in practice.
Point 1 I can implement myself using sed, as I also can get rid of all clear classes and clc, but the options 2 and 3 seem much harder. Is there anything at least remotely resembling options 2 or 3?

How to document a function in Octave?

The publish function in MATLAB works for both scripts and functions,
while the one for Octave works only for scripts: if I enter
publish myFunc.m
Octave gave
error: publish: Only Octave script files can be published.
Am I able to publish a function in Octave? If yes, how?
You can use Octave Forge generate_html package which is meant to generate html of individual functions. It is mostly used to generate the documentation of Octave Forge packages, and its defaults reflect that, but you could add any style you want.
This package will use the same help text that the help function in Octave sees which is the first block of comments in the file that does not start by Copyright or Author. You can have it in plain text but for nicer formatting, you can use Texinfo. In that case, the first line of the help text should be -*- texinfo -*-. There is a page on the Octave wiki with tips on how to write nice help text including a short section on Texinfo syntax (the actual Texinfo manual can be a bit overwhelming).
In addition to the help text, the generate_html package also identifies %!demo blocks and generates a section with the demo code and output it generates, including figures.
The best way to see how help text and demo blocks work in Octave is to check the source (as #Andy pointed out in the comments). For example, see the source for inpolygon (scroll to the bottom to find the %!demo blocks, right before %!test and %!error). The generate_html package generates this page (note the Demonstration blocks).
This is a "multiple questions in one" question, making lots of assumptions in between, so let's address those first:
1. I'll start by the question in the comment, since that's the easiest: Matlab's publisher is not a code documentation tool. It's a "make a quick report that includes both text and code to show at your supervisor meeting or write a quick point in a blog" tool. So the link you point to is simply irrelevant in this case, since that talks about documentation for matlab code.
2. The fact that matlab's publisher also "works for functions", particularly given the first point here, should be considered to be more of a bug than a feature, or at most as a trivial undocumented extension. If you look at the matlab documentation for the publish command, you'll see it expects a filename, not a function with arguments, and the documentation only talks about scripts and makes no mention of 'function' compatibility.
3. Furthermore, even if you did want to use publisher as a "documentation tool", this is counterintuitive for functions in this format, since you need to provide arguments in order for publisher to work (which will not be shown in the actual report), you'll need a modified version that displays intermediate calculations (as opposed to your normal version which presumably does not), and the function just spews an ugly ans= blabla at the end of the report. If your end goal is documentation, it might be best to write a bespoke script for this anyway, showing proper usage and examples, like matlab does in its actual documentation.
Having said all that, yes, there is a 'cheat' you can do to include a function's code in your published report, which is that, in octave (also matlab since R2016b), functions can be defined locally. A .m file that only contains function definitions is interpreted as a function file, but if there are other non-function-declaration instructions preceding the function definitions (other than comments), then it is seen as a script. So if you publish this script, you effectively get a published report with function code in it:
%% Adding function
% This function takes an input and adds 5 to it.
%% Example inputs
In = 10;
%% The function itself
% Marvel at its beauty!
function Out = myfun(In)
%% Here is where the addition takes place.
% It is a beautiful addition
Out = In + 5;
end
%% Example use
Out = myfun(In)
(If you're not happy about having to create a 'wrapper script' manually, you can always create your own wrapper function that does this automatically).
However, both the matlab and octave publishers are limited tools by design. Like I said earlier, it's more of a "quick report to show numbers and plots to your supervisor" tool, rather than a "make nice documentation or professional reports" tool. Instead, I would invest in a nice automated latex workflow, and have a look at code formatting tools for displaying code, and simply wrap that code in a script that produces output to a file that you can then import into latex. It may sound like more work, but it's a lot more flexible and robust in the long term, particularly since the formatting commands can be very quirky as well as limited.

How to include excluded toolbox m-files in MATLAB compiler

I want to use Fuzzy Logic Toolbox in C#. To do this, I created a NET-library using deploytool, but it does not include the file fuzzy.m, which I need to work. And in the log mccExcludedFiles.log the following information:
This file contains the list of various toolbox functions that are not
included in the CTF file. An error will be thrown if any of these functions
are called at run-time. Some of these functions may be from toolboxes
that you are not using in your application. The reason for this is that
these toolboxes have overloaded some methods that are called by your code.
If you know which toolboxes are being used by your code, you can use the -p
flag with the -N flag to list these toolboxes explicitly. This will
cause MATLAB Compiler to only look for functions in the specified toolbox
directories in addition to the MATLAB directories. Refer to the MCC
documentation for more information on this.
C:\Program Files\MATLAB\R2010b\toolbox\fuzzy\fuzzy\fuzzy.m
called by D:\MyFolder\VNTU\bakal\matlabAndCs\ShowFuzzyDesigner.m
(because of toolbox compilability rules)
How do I include this excluded fuzzy.m file in the compilation?
The command fuzzy launches the Fuzzy Inference Systems editor, a GUI supplied with Fuzzy Logic Toolbox. Compilation of Toolbox GUIs with MATLAB Compiler is typically not supported, and as detailed in the documentation for MATLAB Compiler, this is true of the GUIs within Fuzzy Logic Toolbox.
I must say, I think the message you're getting in the mccExcludedFiles.log file is mostly misleading - all of those things could cause a file to have been excluded, but in this case the only relevant bit is at the end, "(because of toolbox compilability rules)".
You might want to look into how to build a fuzzy system using the line code functions supplied with the fuzzy toolbox, and not the GUI. This walkthrough gives you a pretty good handle on building a Mamdani inference system using the line code tools. I am not positive how these translate into C# code, but I think there should be equivalent libraries therein.
If you cannot find a natural way to implement the MATLAB routines in C#, then you could look at this discussion which links some free fuzzy libraries for C#. I think one of the links is broken, but the other three load just fine.

using dll in matlab

i have a problem to using a dll fortran in matlab.
i couldn't use a dll ,that is built by fortran, in matlab. i use "loadlibrary" instruction in matlab but the error is related to header files.
what is header files??
please give me more information to load a dll fortran in matlab and call it.
Rather than try to use a dll file directly I suggest you re-build it using Matlab's MEX functionality. Yes, a mex file is a dll and you can build dlls outside Matlab and use them successfully, it's a lot easier, for a beginner such as I guess you to be, to use MEX. One way in which it is easier is that, if you build a mex file, the system won't ask you for a header file which is, as you know, a rather foreign concept to a Fortran programmer. Another way in which MEX will make your life easier is that you can then call the function exposed by the dll directly from Matlab's command line, without loadlibrary.
Study the Matlab documentation on MEX files, pay particular attention to how to integrate Fortran this way.
Without seeing your header file and the command line you're using in MATLAB, it's hard to help you too much here. You might reference the documentation in MATLAB which request that you pass two arguments to loadlibrary, the second being the header file with function signatures. I am guessing you are not providing this second argument.
You need to provide a header file that defines each of the named functions in the Fortran DLL that you'll be calling. For instance, if your DLL contains a function named sum that sums two double precision variables, like:
function sum(a,b) result(sum)
real(kind=2), intent(in) :: a, b
real(kind=2) :: sum
sum = a + b
end function
Then your header will need to contain something like:
double sum(double*a, double*b);
But don't forget to decorate this with the name mangling specific to your Fortran compiler. For instance, if sum was in a module named foo, and you compiled with gfortran, then you will need something like:
double __foo_MOD_sum(double*a, double*b);
There are a lot of other cases, but that's the gist of it.