Problem using emxArray_real_T from Matlab Coder

31 visualizaciones (últimos 30 días)
Carlos
Carlos el 26 de Ag. de 2013
Editada: Ryan Livingston el 6 de En. de 2015
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!

Respuesta aceptada

Ryan Livingston
Ryan Livingston el 27 de Ag. de 2013
Editada: Ryan Livingston el 6 de En. de 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 comentario
Carlos
Carlos el 27 de Ag. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

Imran
Imran el 5 de En. de 2015
Editada: Imran el 6 de En. de 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;
}

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by