Parsing problems with Matricies in C++ Mex Files

2 views (last 30 days)
Hello~!
I am trying to create a .mex64 file which takes in an Grey Image (or later RGB) with a few other parameters, does a few computations and outputs a vector.
I am stuck with trying to read the image. My output for each point is always 0, and have no idea what I'm doing wrong.
I am using Matlab 2013a with the compiler from Visual Studio 2008.
This is my code:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
size_t m, n;
double *Img;
int cx, cy, a, b, theta, numPoints;
/* Check for proper number of arguments */
if (nrhs != 7) {
mexErrMsgIdAndTxt( "MATLAB:SampleEllipse:invalidNumInputs",
"Seven input arguments required.");
} else if (nlhs > 1) {
mexErrMsgIdAndTxt( "MATLAB:SampleEllipse:maxlhs",
"Too many output arguments.");
}
m = mxGetM(prhs[5]);
n = mxGetN(prhs[5]);
cx = mxGetScalar(prhs[0]);
cy = mxGetScalar(prhs[1]);
a = mxGetScalar(prhs[2]);
b = mxGetScalar(prhs[3]);
theta = mxGetScalar(prhs[4]);
Img = mxGetPr(prhs[5]);
numPoints = mxGetScalar(prhs[6]);
printf("%d %d %d %d %d %d | ptr: %u\r\n", cx, cy, a, b, theta, numPoints, Img);
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
printf("[%d*%d]+%d=%d: %d\n", i, m, j, (i*m) + j, Img[(i*m) + j]);
}
}
}
In matlab, I call it like (mex function is called 'SampleEllipse'):
testArray = [1 2 3; 4 5 6; 7 8 9];
SampleEllipse(1,2,3,4,5,testArray, 12)
and get an output of:
[0*3]+0=0: 0
[0*3]+1=1: 0
[0*3]+2=2: 0
[1*3]+0=3: 0
[1*3]+1=4: 0
[1*3]+2=5: 0
[2*3]+0=6: 0
[2*3]+1=7: 0
[2*3]+2=8: 0
This has me rather confounded and would appreciate any assistance!
Thanks!

Accepted Answer

James Tursa
James Tursa on 4 Feb 2014
You are printing out a double floating point value using an integer format %d. Use a floating point format instead (%f, %g). E.g.,
printf("[%d*%d]+%d=%d: %f\n", i, m, j, (i*m) + j, Img[(i*m) + j]);
  1 Comment
John
John on 4 Feb 2014
Ha doh! Thanks, that did not occur to me as the error (obviously, because I had to ask...).

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!