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

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 Comments

I'm not entirely clear as to what your question exactly is. It looks to me that your problem is more to do with the C language (or C++, not sure exactly which one you're using)
In particular, with
double array[] = {1, 2, 3}
the type of array is double[3]. This is implicitly convertible to double*. In essence, array is like a pointer (to a fixed sized block of memory)
With
double* input;
double array2;
the type of array2 is double. It is not equivalent to a pointer at all. input however is a pointer.
It sounds like you want to pass an array to a function. In order for us to tell you how to call that function, we'd need to see the prototype of that function.
Hi Guillaume, thank you for your answer. What I would like is that if the following is typed in a MATLAB script:
inputMATLAB = [1, 2, 3];
mexfuncton(inputMATLAB);
This vector inputMATLAB would be accesible in the format mexArray [3] = {1, 2, 3} (thus double array [3], right?) in the mex function. Does that make my question clearer?
Matthias
Does that make my question clearer?
Not really, I'm afraid. I don't understand what you mean by would be accesible in the format. What you show is a declaration, so you're not accessing anything.
Note that you can index a pointer using the [] notation:
double* mexArray = mxGetPr(prhs[0]);
double value = mexArray[5]; //as long as the original matlab array had at least 6 elements
Again, it would be clearer if you explained what you're trying to do with that array afterward.
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.
@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.

Sign in to comment.

Answers (1)

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.

Asked:

on 21 Dec 2017

Answered:

on 21 Dec 2017

Community Treasure Hunt

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

Start Hunting!