Reputation: 105
Could someone with more experience advise me how to achieve the above point please?
This must be certainly a trivial matter; however, after many days and nights of trying, researching and reading up on internet I still cannot my head around it.
Setup:
Two C++ source files and a header file:
main.cpp
sub.cpp
sub.hpp
I would be very grateful for any help, hints or constructive comments in advance.
// main.cpp
// MATLAB API Header Files
#include "mex.hpp"
#include "mexAdapter.hpp"
// Custom header
#include "sub.hpp"
// Overloading the function call operator, thus class acts as a functor
class MexFunction : public matlab::mex::Function {
public:
void operator()(matlab::mex::ArgumentList outputs,
matlab::mex::ArgumentList inputs){
matlab::data::ArrayFactory factory;
// Validate arguments
checkArguments(outputs, inputs);
double* darrY = matlab::data::TypedArray<double>(std::move(inputs[0])).release().get();
double* darrD = matlab::data::TypedArray<double>(std::move(inputs[1])).release().get();
const double csT = inputs[2][0];
const double csKy = inputs[3][0];
// data type of outp is "just" a plain double, NOT a double array
double outp = foo(darrY, darrD, csT, csKy);
outputs[0] = factory.createScalar(outp);
void checkArguments(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs){
// Create pointer to MATLAB engine
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
// Create array factory, allows us to create MATLAB arrays in C++
matlab::data::ArrayFactory factory;
// Check input size and types
if (inputs[0].getType() != ArrayType::DOUBLE ||
inputs[0].getType() == ArrayType::COMPLEX_DOUBLE)
{
// Throw error directly into MATLAB if type does not match
matlabPtr->feval(u"error", 0,
std::vector<Array>({ factory.createScalar("Input must be double array.") }));
}
// Check output size
if (outputs.size() > 1) {
matlabPtr->feval(u"error", 0,
std::vector<Array>({ factory.createScalar("Only one output is returned.") }));
}
}
};
// sub.cpp
#include "sub.hpp"
#include "armadillo"
double foo(double dY[], double dD[], const double T, const double Ky) {
double sum = 0;
// Conversion of inputs parameters to armadillo matrices using the armadillo's so called advanced matrix constructor:
// mat(ptr_aux_mem, n_rows, n_cols, copy_aux_mem = true, strict = false)
// Fixme: parameterize n_rows, n_colss
arma::mat mY(&dY[0], 2, 168, false);
arma::mat mD(&dD[0], 2, 168, false);
// Armadillo calculations
for(int t=0; t<int(T); t++){
// some armadillo based calculation
// each for cycle increments sum by its return value
}
return sum;
}
// sub.hpp
#ifndef SUB_H_INCLUDED
#define SUB_H_INCLUDED
double foo(double dY[], double dD[], const double T, const double Ky);
#endif // SUB_H_INCLUDED
Upvotes: 2
Views: 1106
Reputation: 1466
There is a similar question at MATLAB Central. Changing the lines
double* darrY = matlab::data::TypedArray<double>(std::move(inputs[0])).release().get();
double* darrD = matlab::data::TypedArray<double>(std::move(inputs[1])).release().get();
to
TypedArray<double> matrix1 = std::move(inputs[0]);
TypedArray<double> matrix2 = std::move(inputs[1]);
buffer_ptr_t<double> Y = matrix1.release();
buffer_ptr_t<double> D = matrix2.release();
double* darrY = Y.get();
double* darrD = D.get();
seems to solve the problem. When I debugged your code it looked like both matrices got the same address for some reason.
Upvotes: 3