MEX passing Integer - wrong results
10 views (last 30 days)
Show older comments
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.
2 Comments
Jan
on 14 Nov 2017
Please post the relevant part of the code. Especially a description like "I am doing the very same thing with a smaller integer" is not useful to reconsider, what the problem is.
int a
a = (int) *mxGetPr(prhs[3]);
Should work. I'd prefer:
a = (int) mxGetScalar(prhs[0]);
because then you can use inputs of any type also. Perhaps the 4000 you are providing is a int32 in Matlab?
Answers (2)
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.
3 Comments
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?
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.
0 Comments
See Also
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!