Reputation: 902
I am new in mex type programming, basically I am writting my first mexfunction, and I am encountering a silly problems. I attach a part of my code:
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int mrows,ncols;
mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);
int numElements = ncols;
size_t size = numElements * sizeof(float);
float *in_A = (float *)mxMalloc(size);
float *in_B = (float *)malloc(size);
float *out_C = (float *)malloc(size);
if (in_A == NULL || in_B == NULL ||out_C == NULL)
{
mexErrMsgTxt("Failed to allocate host vectors!\n");
}
if (nrhs != 2)
mexErrMsgTxt("Two inputs required. A vector and a multiplier");
if (nlhs != 1)
mexErrMsgTxt("One output required. The resulting vector");
in_A = (float *)mxGetPr(prhs[0]);
in_B = (float *)mxGetPr(prhs[1]);
//printf("%f %f\n", in_A[2], in_A[1]);
//mexPrintf("%d\n", prhs[1]);
plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
out_C = (float *)mxGetPr(plhs[0]);
add_two_vectors(in_A,in_B,out_C);
}
The function add_two_vectors(float* A,float* B,float* C)
is working but when I am running the script inside a mexfunction I have a problem. Also it reads correctly the nrows and ncols variables. The problem is that I can not insert the correct inputs in the function. I have tried to print some of the vector elements but it prints different elements than the input vector elements. I am calling mexfunction like that a = addVector([1.1 2.2 355],[2.5 45 5.5])
. Can someone enlighten me??
Thank you in advance.
PS: Matlab 2011a, VS 2010, Win 7 64x.
Upvotes: 2
Views: 1581
Reputation: 30589
You can't just static cast the output of mxGetPr
(a double*
) to a float*
without misinterpreting the data. Read in the contents as double and convert each element, not the pointer.
Alternatively, you can input a float array with single(...)
on the input in MATLAB, and use (float *)mxGetData
instead of mxGetPr
.
Upvotes: 0
Reputation: 124563
Let me give an example how I would implement such a function:
#include "mex.h"
void add_two_vectors(float *c, const float *a, const float *b, const size_t sz)
{
for (size_t i=0; i<sz; i++) {
c[i] = a[i] + b[i];
}
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// input validation
if (nrhs != 2 || nlhs > 1) {
mexErrMsgTxt("Wrong number of input/output arguments.");
}
if (!mxIsSingle(prhs[0]) || !mxIsSingle(prhs[1])) {
mexErrMsgTxt("Inputs must be single arrays.");
}
if (mxIsComplex(prhs[0]) || mxIsComplex(prhs[1])) {
mexErrMsgTxt("Inputs must be real arrays.");
}
if (mxIsSparse(prhs[0]) || mxIsSparse(prhs[1])) {
mexErrMsgTxt("Inputs must be dense arrays.");
}
if (mxGetNumberOfElements(prhs[0]) != mxGetNumberOfElements(prhs[1])) {
mexErrMsgTxt("Inputs must have the same size.");
}
// create ouput array
mwSize numel = mxGetNumberOfElements(prhs[0]);
mwSize ndims = mxGetNumberOfDimensions(prhs[0]);
const mwSize *dims = mxGetDimensions(prhs[0]);
plhs[0] = mxCreateNumericArray(ndims, dims, mxSINGLE_CLASS, mxREAL);
// get pointers to data
float *c = (float*) mxGetData(plhs[0]);
float *a = (float*) mxGetData(prhs[0]);
float *b = (float*) mxGetData(prhs[1]);
// perform addition: c = a + b
add_two_vectors(c, a, b, numel);
}
The function expects two arrays of type single
of the same size (but can be of any dimensions including scalars, vectors, matrices, N-D arrays).
>> mex -largeArrayDims add_vectors.cpp
>> add_vectors(rand(4,'single'),rand(4,'single'))
ans =
0.6204 1.2307 1.1657 0.7583
1.3147 0.3817 0.3059 1.0377
0.8142 0.3519 0.8619 1.1154
1.0403 1.1578 0.1365 1.0048
Upvotes: 1