Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: mex function for beginners
Replies: 13   Last Post: Aug 3, 2011 12:52 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Manu

Posts: 2
Registered: 8/3/11
Re: mex function for beginners
Posted: Aug 3, 2011 12:52 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

What probably happened in your case was that you were not typecasting the data to integer in matlab itself? You were passing it as it from matlab. The default data type in matlab is double. This means that you passed in a double matrix to your C mex function. Now when you use mxGetPr it returns you a double pointer. Now If you type cast this double pointer to int pointer, it is going to give you the wrong result when you dereference it because numbers are stored differently in floating point format and in integer fomat. Double uses 8 bytes normally while int uses 4. NOw when you derefernce the double pointer as int, it picks up first four bytes which correspond to only the sign bit and the exponent bits or maybe some precision bits but the complete number is not getting converted to int.

YOu possibly would have gotten the wrong result if you would have used mxGetData also without using the typecasting in matlab. Even though mxGetData returns a void pointer, the data actually is double so again by typecasting it as integer what I wrote above would happen.

The key thing to note in this is that you needed to typecast to integer in matlab itself before passing it to your C mex function.

"basudha pradhan" wrote in message <fbc95u$o4q$1@fred.mathworks.com>...
> Thanks Dan for the suggestion. I tried mxGetdata but i got
> this error when compiling the file.
>

> >> mex bitplan.c
> Microsoft (R) Incremental Linker Version 7.10.3077
> Copyright (C) Microsoft Corporation. All rights reserved.
>
> C:\DOCUME~1\Owner\LOCALS~1\Temp\bitplan.obj
> Creating library _lib3547.x and object _lib3547.exp
> bitplan.obj : error LNK2019: unresolved external symbol
> _mxGetdata referenced in function _mexFunction
> bitplan.mexw32 : fatal error LNK1120: 1 unresolved
> externals
>
> C:\PROGRAM FILES\MATLAB71\BIN\MEX.PL: Error: Link
> of 'bitplan.mexw32' failed.
>
> ??? Error using ==> mex
> Unable to complete successfully
>
>




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.