C++ library class method does not return MATLAB array type even when "ReturnCArrays" set to false when generating library definition file with clibgen
4 views (last 30 days)
Show older comments
Hello,
I would like to instantiate some C++ classes in MATLAB (using the clib and clibgen packages) and had some difficulties when trying to call some methods that should return an MATLAB type array to MATLAB.
In fact, by default, C++ methods returning a pointer to an array, or a std::vector of primitive types are returned to MATLAB as of type clib.array.*.*, which is:
- quite inconvenient when one wants to do some further processing in MATLAB with the returned array
- forces me to cast it to MATLAB primitive type array AND to transpose it if output is expected to be a column vector.
For this reason, I set "ReturnCArrays" to false when generating the library defintion file with clibgen.generateLibraryDefinition(). However the generated library definition still defines a returned array of type clib.array.*.* (and not a MATLAB array as expected).
Here is a small example that demonstrates this problem:
Test Script in MATLAB
% generate Library definition
clibgen.generateLibraryDefinition("test.hpp", ...
"PackageName", "testPackage", ...
"ReturnCArrays", false, ...
"Verbose", true);
% build interface
build(definetestPackage);
addpath testPackage/;
% instantiate dummy object
myObj = clib.testPackage.TestClass();
% call test method
myVec = myObj.createSuperVector();
% myVec is returned as a "clib.array.testPackage.Double"
% What I could do, but want to avoid:
% > myVec = double(myVec(:));
C++ class definition (test.hpp)
#include <vector>
class TestClass {
public:
TestClass(){};
~TestClass(){};
std::vector<double> createSuperVector() {
// instantiate a 3 element vector with values 1.234
std::vector<double> superVector(3, 1.234);
return superVector;
}
};
Note that if the method createSuperVector() was defined to return a double*, the parameter "ReturnCArrays" set to false does the job and a double array (of MATLAB type) is returned to MATLAB.
Some might say that I could maybe rewrite my C++ library such that the method returns a double*, however, as I understood, the dimensions of the output array have to be defined in the .mlx library definition file (either as fixed values or by using methods parameter names, e.g. ["len", 1] for a column vector of len elements).
So, in the case the output size is not fix, I would have to define the method as
const double* createSuperArray(int len) {
double *superArray = new superArray[someObjectAttributeThatDefinesTheSize];
// some code
return superArray;
}
and modify the .mlx library definition file:
defineOutput(createSuperArrayDefinition, "RetVal", "double", ["len", 1]);
Then, in MATLAB, the user will have to call the method this way
le = 3;
myArr = myObj.createSuperArray(len);
% (!) very dangerous --> seg. fault if user does not know what he is doing
Which is quite ugly and dangerous, as the user should make sure that the input parameter len is equal to the value of someObjectAttributeThatDefinesTheSize.
Does anybody knows how to return an array (of MATLAB type) from a vector<double> method? If so, would it then be possible to generalize it to vector<vector<double>> methods? Alternatively for the case of a double* methods, would it be possible to use other variables than one of the method parameters (e.g. a property of the C++ object) for setting the dimensions of an output array in the function defineOuput() of the .mlx file?
Sorry for the tricky question :) I hope I am just too tired and the solution is obvious...
Thank you very much !
FZ
1 Comment
Boguslaw Wiecek
on 22 Sep 2023
Is there an answer to this question? I need to convert clib.array to standard MATLAB array in matlab, but short of copying element by element (which for large arrays takes enormous amount of time) there seems to be no way to convert.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!