MATLAB: ‘Index exceeds matrix dimensions’ when running a very simple mex function I wrote

mex

I came into 'Index exceeds matrix dimensions' issue when running a very simple mex function I created. This function is called mk_uint32. It shifts the four elements 24,16,8 and 0 bits to the left to create a uint32 number.
Here is the commands I keyed in on the command line:
a=uint32([1 2 3 4]);
b=mk_uint32(a);
and I got the following error message:
Index exceeds matrix dimensions.
Please help me figure out what is wrong. Thanks ahead.
Here is the c code called 'mk_uint32.c'.
#include "mex.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
/* Function Definitions */
uint32_T mk_uint32(uint32_T *indata, uint32_T *outdata)
{
outdata = (indata[0] << 24U) + (indata[1] << 16U) + (indata[2] << 8U) + indata[3];
}
/* The gateway routine */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
uint32_T *x,*y;
if (nrhs != 1) mexErrMsgTxt("One input required.");
if (nlhs != 1) mexErrMsgTxt("One output required.");
/* Create a pointer to the input matrix x. */
x = mxGetPr(prhs[0]);
/* Set the output pointer to the output matrix. */
plhs[0] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL);
/* Create a C pointer to a copy of the output matrix. */
y = mxGetPr(plhs[0]);
/* Call the C subroutine. */
mk_uint32(x,y);
}

Best Answer

In mk_uint32, outdata is a pointer, but you are setting that pointer to a numeric value. You need to store the result in outdata[0] or in *outdata