How to return array from imported C++ code in C function block in Simulink?

9 views (last 30 days)
Hello everyone!
I am trying to import C++ code in Simulink through the C function block, for my purposes the block has 6 inputs type double, and 7 outputs type array of 9 doubles. I have been trying to run some dummy code in order to grasp how to use this block but I have been stuck for some time when trying to return an array from a C++ function and assigning it to the output array variable.
Here is the header file for the dummy class.
#pragma once
#include <vector>
#include <deque>
#include <array>
#include <cmath>
#include "mex.h"
class dummy{
double x, y, psi, v, beta, dpsi;
public:
dummy(){}
dummy(double x, double y, double psi, double v, double beta, double dpsi);
double getX(){return x;}
double getY(){return y;}
double getPsi(){return psi;}
double getV(){return v;}
double getBeta(){return beta;}
double getDPsi(){return dpsi;}
};
extern void createDrift(double x, double y, double psi, double v, double beta, double dpsi);
extern void deleteDrift();
extern mxArray* DriftOutput(double x, double y, double psi, double v, double beta, double dpsi)
And here is the source file.
#include <vector>
#include <deque>
#include <array>
#include "mex.h"
#include "dummy.h"
dummy* var;
dummy::dummy(double x, double y, double psi, double v, double beta, double dpsi){
this->x = x;
this->y = y;
this->psi = psi;
this->v = v;
this->beta = beta;
this->dpsi = dpsi;
}
extern void createDrift(double x, double y, double psi, double v, double beta, double dpsi){
var = new dummy(x,y,psi,v, beta, dpsi);
}
extern void deleteDrift(){
delete var;
}
extern mxArray* DriftOutput(double x, double y, double psi, double v, double beta, double dpsi){
std::vector<double>vals(x,9);
mxArray* temp = mxCreateNumericMatrix( 1, 9, mxSINGLE_CLASS, mxREAL );
//memcpy( mxGetData(temp[0]), &vals[0], 9*sizeof(double) );
/*temp.push_back(std::vector<double>(var->getX(),9));
temp.push_back(std::vector<double>(var->getY(),9));
temp.push_back(std::vector<double>(var->getPsi(),9));
temp.push_back(std::vector<double>(var->getV(),9));
temp.push_back(std::vector<double>(var->getBeta(),9));
temp.push_back(std::vector<double>(var->getDPsi(),9));
temp.push_back(std::vector<double>(var->getX(),9));*/
return temp;
}
And this is the code from Output section of C function block
mxArray* temp = DriftOutput(x, y, psi, v, beta, dpsi);
xVec = temp;
Error that I get with this is
In line 3:error: 513, a value of type "mxArray *" cannot be assigned to an entity of type "real_T *"
| xVec = temp;
| ^
Has anyone encountered this problem before? Any tips on how to solve this?
I have tried to assign temp[0] to xVec, but that too ends in failure.

Answers (1)

Mark McBroom
Mark McBroom on 16 Mar 2023
Why is your C++ code deailing with mxarray? The whole point of the C Function block is that behind the scenes it will generate wrapper code around your C++ code to handle transfer of data between Simulink and the C++ code ( which will likely involve using mxarray). I would recommend modifying your C++ code to remove the include of mxarray and rewrite your DriftOutput function.
Also, this link has tips for using the C Function block with C++ code.https://www.mathworks.com/help/simulink/ug/interface-with-c-classes-using-c-function-block.html

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!