Borrar filtros
Borrar filtros

How can I create a double array in a C++ mex file from a matlab vector input?

7 visualizaciones (últimos 30 días)
Hi,
I would like to be able to access a double array in my mex file that is provided as a vector input argument in MATLAB. That is, the vector should be converted to the same format as an input that is currently (hard coded) in the file as:
double array[] = {1, 2, 3}
My current best guess is something like this, but it doesn't seem to work.
double *input;
double array;
input = mxGetPr(prhs[0]);
array2 = *input;
I have extensively looked at the examples provided in MATLAB, but I can't find the answer. Any help is highly appreciated!
More generally, I would like to import a MATLAB structure containing several input fields (among which vectors) into C++. Being new to C, have come to the conclusion that there is no straightforward way to do this. Is this correct? Any advice on how to do this is also highly appreciated!
Matthias
  5 comentarios
Matthias
Matthias el 21 de Dic. de 2017
I need the array as input for a function. Currently the cpp file looks like this:
double input [3] = {1, 2, 3};
double *outputvalue;
*outputvalue = functionwithinmexfile(input);
However, I would like to be able to feed the mex function a MATLAB vector, that will then function as input to functionwithinmexfile.
James Tursa
James Tursa el 21 de Dic. de 2017
Editada: James Tursa el 21 de Dic. de 2017
@Matthias: Do what Guillaume suggests. I.e.,
double *mexArray = mxGetPr(prhs[0]);
:
*outputvalue = functionwithinmexfile(mexArray);
You should also check to see that plhs[0] is exactly the size and class that you are expecting before you start dereferencing pointers associated with it. E.g., put something like this up front:
if( nrhs == 0 || !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
mxIsSparse(prhs[0]) || mxGetNumberOfElements(prhs[0]) != 3 ) {
mexErrMsgTxt("Input must be full real double 3-element vector");
}
If you start having questions about how to access fields of an input struct etc, I would suggest opening up a new Question for that.

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 21 de Dic. de 2017
Whatever the signature of your function i.e. one of:
double* functionwithinmexfile(double* in);
//or
double* functionwithinmexfile(double in[]);
//or
double* functionwithinmexfile(double in[3]);
in decays to a pointer (and in the last case, the [3] is misleading as it is ignored by the compiler), so there is no issue passing the pointer returned by mxGetPtr directly to the function (as long as you've verified that it is indeed a pointer to a double array with enough element). So:
double* mexArray = mxGetPr(prhs[0]);
double* out = functionwithinmexfile(double* in);
Note that it's rather unusual for a function to take a pointer to an array as an input without having the size of array also has an input. Normally the signature would be
double* functionwithinmexfile(double* in, size_t numelems);
Otherwise, the only safe thing that the function can do is return the in pointer as an output without ever derefencing it. Anything else is just asking for buffer underflow/overflow bugs.
And hopefully, that's C code not C++ code despite your question title because a C++ function should never return a raw pointer, it should return a std::unique_ptr<double> or a std::vector<double> or something similar.

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by