Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: How to use the double array for indexing in MEX file

Subject: How to use the double array for indexing in MEX file

From: Petr Krysl

Date: 13 Feb, 2008 17:14:02

Message: 1 of 5

For you MEX-experts this must be easy: How do I use an
array, which in Matlab is of double precision, for indexing
into another array inside a Mex function? And what if the
array was created as 'int64', how do I use it then? There
does not seem to be any equivalent to mxGetPr for this
datatype...

If you have any source of information that you can recommend
of these aspects, that would be much appreciated.

Petr

Subject: How to use the double array for indexing in MEX file

From: Peter Boettcher

Date: 13 Feb, 2008 18:03:07

Message: 2 of 5

"Petr Krysl" <pkryslNOSP@Mucsd.edu> writes:

> For you MEX-experts this must be easy: How do I use an
> array, which in Matlab is of double precision, for indexing
> into another array inside a Mex function? And what if the
> array was created as 'int64', how do I use it then? There
> does not seem to be any equivalent to mxGetPr for this
> datatype...
>
> If you have any source of information that you can recommend
> of these aspects, that would be much appreciated.

If the array really is double, then extract each element in the usual
way, then cast it to int:

double *index_array_double;
int index;

index_array_double = mxGetPr(prhs[0]);

for(i=0; i<N; i++) {
  index = (int) index_array_double[i];
  // use index
}


If the array is some other datatype, you can cast the pointer output
from mxGetData:

long long int *index_array_int64;

index_array_int64 = (long lont int*) mxGetData(prhs[0]);

index_array_int64[0] is the first element


mxGetData() does the same thing as mxGetPr(), it just returns a void *
type instead of a double *. Both are pointers to the data buffer of the
variable. You just have to tell C how to interpret the pointer.

-Peter

Subject: How to use the double array for indexing in MEX file

From: Petr Krysl

Date: 14 Feb, 2008 00:36:02

Message: 3 of 5

Peter Boettcher <boettcher@ll.mit.edu> wrote in message
<muyy79o20pw.fsf@G99-Boettcher.llan.ll.mit.edu>...
> "Petr Krysl" <pkryslNOSP@Mucsd.edu> writes:
>

> If the array really is double, then extract each element
in the usual
> way, then cast it to int:

Thanks, much obliged.

Petr

Subject: How to use the double array for indexing in MEX file

From: Hamid

Date: 9 Jun, 2008 21:04:02

Message: 4 of 5


> long long int *index_array_int64;
>
> index_array_int64 = (long lont int*) mxGetData(prhs[0]);
>
> index_array_int64[0] is the first element
>
>
> mxGetData() does the same thing as mxGetPr(), it just
returns a void *
> type instead of a double *. Both are pointers to the data
buffer of the
> variable. You just have to tell C how to interpret the
pointer.
>
> -Peter

I have tried this method (casting) to send an uint32 data
from Matlab to MEX but then when I want to access my data in
MEX it's just garbage. If I use mxGetPr and convert the
input uint32 to double in Matlab then everything works.

Any other steps I need to take before or after doing the
explicit casting in MEX files.


Subject: How to use the double array for indexing in MEX file

From: James Tursa

Date: 9 Jun, 2008 23:13:01

Message: 5 of 5

"Hamid " <spam.wax@gmail.com> wrote in message <g2k5s2
$4on$1@fred.mathworks.com>...
>
> > long long int *index_array_int64;
> >
> > index_array_int64 = (long lont int*) mxGetData(prhs
[0]);
> >
> > index_array_int64[0] is the first element
> >
> >
> > mxGetData() does the same thing as mxGetPr(), it just
> returns a void *
> > type instead of a double *. Both are pointers to the
data
> buffer of the
> > variable. You just have to tell C how to interpret the
> pointer.
> >
> > -Peter
>
> I have tried this method (casting) to send an uint32 data
> from Matlab to MEX but then when I want to access my
data in
> MEX it's just garbage. If I use mxGetPr and convert the
> input uint32 to double in Matlab then everything works.
>
> Any other steps I need to take before or after doing the
> explicit casting in MEX files.
>

Can you post some code to show us what did and didn't work
for you? I agree with Peter, you should only use mxGetPr
for inputs that are known to be double, and you should use
mxGetData for all the other classes. Using mxGetData with
a uint32 class should work just fine as long as you do
your casting correctly.

James Tursa


Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
willix Willix Halim 20 Feb, 2008 20:12:33
indexing with double precision array Petr Krysl 13 Feb, 2008 12:16:29
mex Petr Krysl 13 Feb, 2008 12:16:29
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics