Automatic Generation of Inputs Information for MATLAB Coder
Centre de Recherche en Informatique (CRI)
MINES ParisTech - PSL
MATLAB Coder indded enables users to get C/C++/Mex version of their MATLAB routines. However, and this makes sense, we need to specify for each input its name, type, size bounds. This looks like the following
assert(isa(col,'double')); assert(all(size(col) >= [1000 1])); assert(all(size(col) <= [2000 1]));
The above line says that variable col is of a 2D (indeed 1D) array of between 1000 and 2000 values of type double. This will certainly helps MATLAB Coder to make correct assumptions and memory allocations, it is mandatory.
If you have lot of inputs including complex structures, then the task becomes tedious. Indeed, for a structure (type struct), you need to do the same inventory for all its fields, and recurse if necessary. For the sizes, you could estimate, but you probably need to run the code to get the right bounds.
Once you get all the information, then you need to write the corresponding lines as previously shown. Its gives something like this
function [pts, nb_pts] = Compute(lig, col)
assert(isa(lig,'double')); assert(all(size(lig) >= [1000 1]));assert(all(size(lig) <= [2000 1]))
assert(isa(col,'double')); assert(all(size(col) >= [1000 1]));assert(all(size(col) <= [2000 1]));
Tool for automatic generation
Here we provide a tool that will snoop your routine, collect/compute all required information and generate the assert lines that you will use within your MATLAB code to instruct MATLAB Coder.
Dowload the code here and copy into your working directory (please, reference this page when using the tool).
The main steps to use it are the followings:
f Consider a matlab function Outputs = MyFunction(Inputs_List)
% 1. Add the following line just below the header of the function
% T = GenDataAssert('','',Inputs_List);
% 2. Delete (if exists) the file InputForMatlabCoder.txt
% 2. Run you code as usual (it will be slower because of file operations)
% 3. The result is in the file InputForMatlabCoder.txt (you could look at the text form in T)
% 4. Just copy the content of the previous file below the #codegen pragma
% 5. Run MATLAB Coder for code generation