I am trying to build a DLL in C++ and call it from MATLAB by using loadlibrary
and calllib
instructions. It works for one value and it return a value normally, but now I am trying to return a whole array from C++ DLL to MATLAB as an output of a function.
As you know C++ usually return the arrays as pointers but this don't work with MATLAB ... I searched for that at the internet and they are using some MEX function but it is not clear ...
Can you explain how to return an array from C++ DLL to MATLAB calllib
and how should we return it from C++ code ?
Consider a DLL that exposes the following C function:
It takes an array already allocated an its length, and fills it with incremental values.
In MATLAB, first we load the library:
To call the exposed function, we use
libpointer
:we could also simply pass regular vectors and MATLAB will take care of marshalling:
note that in this case, the modified array will be returned as an output (since builtin types will not be modified in-place).