Encapsulate the Interface to an External C Dynamic Library. This task is described in the Matlab web page:
However when you try to extend this example to accept more generic input, let's say variable size arrays instead of scalar doubles, it seems necessary to use external routine in order to make it work.
The solution presented not only avoid writing extra source code in C, but allow by minimal change of the source code in Matlab to achieve parameters passage to the encapsulated dll by reference without local copy.
Warning: this example was build with Matlab R2016a. It might no longer compatible to never version of Matlab.
Stefano Gianoli (2020). Encapsulate Interface to an External C Library (https://www.mathworks.com/matlabcentral/fileexchange/59163-encapsulate-interface-to-an-external-c-library), MATLAB Central File Exchange. Retrieved .
Link in description is now broken.
also possible adding a method (Static or not) to AdderAPI (in the case of a Static method):
function Out = adder_trampoline(In_)
Out = coder.opaque('emxArray_real_T*','NULL');
Out = coder.ceval('emxCreateWrapper_real_T', coder.opaque('double*','&In_->data'), int32(size(In_,1)), int32(size(In_,2)));
and then modifying the appropiate lines in AdderAPI.adder:
a1 = AdderAPI.adder_trampoline(a);
b1 = AdderAPI.adder_trampoline(b);
c1 = AdderAPI.adder_trampoline(c);
coder.ceval('adder', a1, b1, c1);
NOTE: the method should be 'inline' otherwise the local variable 'In_' disappears and the source code will not compile (although the Matlab Coder will not issue any error message).
Matlab moved the content to a new link "Encapsulate the Interface to an External C Dynamic Library":