MEX passing Integer - wrong results
11 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2017-11-14
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?
回答(2 个)
Jan
2017-11-14
编辑:Jan
2019-11-13
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 个评论
Jan
2018-8-29
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
2019-11-13
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!