77 views (last 30 days)

Hello,

I have a C++ code that I try to embed in Matlab using a mex function. Everything worked just fine, till I tried to hand over integers from Matlab.

Suppose I call my Mex function like:

myMexFun(0.1, 0.1, 30, 4000);

Apparently it makes a difference if I use: (1)

int a = (int)*mxGetPr(prhs[3]);

instead of defining it directly in my C++ code with: (2)

int a = 4000;

Since the first (1) expression will yield very far off wrong results.

a is used for all calculating operations (+,-,*,/) as well as an index for arrays.

Additionally I am doing the very same thing with a smaller integer for another parameter prhs[2] - (range 5-150) and with this input I do not get any issues.

What am I overseeing here?

Kind regards, and thanks for your time.

Jan
on 14 Nov 2017

Edited: Jan
on 13 Nov 2019

This sounds magic. Whenever magic things appear in discussions in the forum, or at programming in general, it is a secure signal, that the author has overseen an important detail. To find the source of the problem, create a MWE: Minimal working example.

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

int a = (int) *mxGetPr(prhs[3]); // Better: *mxGetData(prhs[3])

mexPrintf("%i\n", a);

mexPrintf("%g\n", (double) a);

mexPrintf("%g\n", mxGetScalar(prhs[3]));

}

Try it. I expect, that the correct values are displayed. This would mean, that perhaps your observation is wrong: How do you find out, that the value of a differs from your expectations? Maybe this test is the only problem.

Jan
on 29 Aug 2018

In your former comment (link) you write: "Which is wrong!" I still do not have any idea about what you consider as wrong here. Without seeing the code, it is impossible to find the cause of the problem. Therefore I can only guess, that an uninitialized pointer is used anywhere.

"sometimes those arrays weren't fully filled with new values" - please elaborate this.

"with the new compiler version, I get the error that some integers which I am passing through the Matlab mex function are not constant" - Please post the specific line and a copy of the error message. The problem is not, that the integer is not constant, but that the variable is not declared as const. So what about casting it to const?

Sign in to comment.

Nope
on 13 Nov 2019

From what I understand, mxGetPr() always returns the pointer as type mxDOUBLE_CLASS*. If you pass an integer the typecasting in this line:

const int a = (int)*mxGetPr(prhs[3]);

should interpret your integer as a double and then convert that interpretation to an integer.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/366939-mex-passing-integer-wrong-results#comment_504628

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/366939-mex-passing-integer-wrong-results#comment_504628

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/366939-mex-passing-integer-wrong-results#comment_504644

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/366939-mex-passing-integer-wrong-results#comment_504644

Sign in to comment.