Problem using emxArray_real_T from Matlab Coder

25 views (last 30 days)
Hi all,
I am trying to learn how to use standalone C++ code from Matlab. I have created a very simple function that takes only one argument, which is a vector of size (N,1), where N is unknown a priori. Therefore, I have to use dynamic allocated memory, if I am not wrong. The generated C++ code takes two arguments, of type emxArray_real_T, where x is the input vector, and yout the output vector,
void fun4(const emxArray_real_T *x, emxArray_real_T *yout)
{
...
}
Now, I am trying to create the input vector, and to run the function, but I think that I have no idea how to do it. I have read the answer in http://www.mathworks.com/matlabcentral/answers/67237-matlab-coder-segmentation-fault , but I think it is not so clear to me. This is what I am trying to do,
int main()
{
double datax[3];
double result[3] = {};
emxArray_real_T *inputs, *outputs;
for (int n=0; n<3; n++)
{
datax[n] = n;
}
inputs = emxCreateWrapper_real_T(&datax, 1, 3);
outputs = emxCreateWrapper_real_T(&result, 1, 3);
fun4(inputs, outputs);
return 0;
}
but I always get compilation errors,
cannot convert ‘double (*)[3]’ to ‘real_T* {aka double*}’ for argument ‘1’ to ‘emxArray_real_T* emxCreateWrapper_real_T(real_T*, int32_T, int32_T)’
Since I am not an expert on C++, I was wondering if anyone can teach me how to pass the input vector, and to recover the results... Thank you very much in advance!

Accepted Answer

Ryan Livingston
Ryan Livingston on 27 Aug 2013
Edited: Ryan Livingston on 6 Jan 2015
Try using:
emxCreateWrapper_real_T(datax, 1, 3)
The compiler is complaining because
datax
is usually treated as a pointer to the first element of datax. Therefore
&datax
is a pointer to an array of 3 doubles rather than a pointer to a single double as fun4 requires. This is the difference between
double (*)[3] //pointer to array of 3 doubles
and
double * //pointer to one double
  1 Comment
Carlos
Carlos on 27 Aug 2013
It worked!! Thank you very much! I thought I was driving crazy. Although, honestly, I was expecting something much more complicated... so now I feel a bit stupid :D As you can see, I am not really quite knowledgeable about C++ yet.

Sign in to comment.

More Answers (1)

Imran
Imran on 5 Jan 2015
Edited: Imran on 6 Jan 2015
I have the same Issue
In my case I need to get out put as a Array of 2 rows and six column
so I am facing the the problem in line
outputs = emxCreateWrapper_real_T(data, 6, 2);
Could you please guide me.
I am currently running program as follows
int main()
{ double strem[12];
double data[2][6];
emxArray_real_T *inputs, *outputs;
for (int n=0; n<13; n++)
{
strem[n] = n;
}
inputs = emxCreateWrapper_real_T(strem, 1,12);
outputs = emxCreateWrapper_real_T(data, 6, 2);
charrang(inputs, outputs);
return 0;
}

Products

Community Treasure Hunt

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

Start Hunting!