How to pass binary values to mex
Show older comments
Hello, I want to transmit 1x12 binary data to mex file for data transmission
b=70.19
Y = round(b,1);
dec= Y*10;
integers=dec;
temp = integers;
mask = temp < 0;
temp(mask) = 2^12 + temp(mask) ;
a=decimalToBinaryVector(temp, 12);
The output binary values (a) is : 0 0 1 0 1 0 1 1 1 1 1 0
When I pass these values to mex file,
b= mex(a);
I get a different binary value. The output of a is:
1.18870000000000e-320 1.18870000000000e-320 1.00000000000053 1.18870000000000e-320 1.00000000000053 1.18870000000000e-320 1.00000000000053 1.00000000000053 1.00000000000053 1.00000000000053 1.00000000000053 1.18870000000000e-320
mex.c
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
double *a;
int M,N;
double* bb; /* 1xN output matrix */
const mwSize *dims;
a=mxGetData(prhs[0]);
plhs[0] = mxCreateDoubleMatrix(M, N, mxREAL);
bb = mxGetPr(plhs[0]);
analogoutputMatlab(a,bb,N);
}
Why do I get change in binary values? How to pass binary values to mex?
14 Comments
James Tursa
on 3 Mar 2020
M and N are not set to values, they are garbage. Also, what is the function signature (types of arguments) for analogoutputMatlab( )?
A R
on 3 Mar 2020
James Tursa
on 3 Mar 2020
It is hard to debug when we are only given code snippets. We don't have all of the function signature information, we don't have all the variable types, etc. So we end up guessing what might be the problem. Can you post complete code for us to see?
Walter Roberson
on 4 Mar 2020
I do not see any signature for mexfile()?
The decimalToBinaryVector function is not documented as returning any values, and especially the data type of what it returns is not documented. It probably returns double through (the output of the examples would look slightly different otherwise.)
It is not good to rely on the idea that a value rounded to the nearest 1/10, when multiplied by 10, would give you an exact integer. It does happen to be true between -100 and 100, but it is risky.
James Tursa
on 4 Mar 2020
Signatures of cbAOut( ) and cbAIn( )? Shouldn't you be including prototypes for these?
A R
on 4 Mar 2020
James Tursa
on 4 Mar 2020
So, if you don't have prototypes, how can I (or the compiler) check to see that your arguments are correct?
A R
on 4 Mar 2020
Walter Roberson
on 4 Mar 2020
if(nrhs=0)
That = is an assignment in C, not a comparison.
A R
on 4 Mar 2020
A R
on 5 Mar 2020
Walter Roberson
on 5 Mar 2020
To be honest, I am not enthusiastic about rebooting my computer into Windows, doing hours and hours of Windows updates, downloading the Measurement Computing development library, installing a compiler, putting together the pieces of what you posted to try to produce a project, and figuring out how to use a Windows debugger.
James would probably appreciate if you put together a zip of everything together and a link of the Measurement Computing development package.
My personal suspicion is that you could perhaps produce a smaller example that did not call upon the measurement computing routines. And if you were to find that the trimmed example does not produce the same problem, then you would have isolated down to an interaction with MC libraries, which would give you information about what to look for.
A R
on 5 Mar 2020
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!