From: Fernando on
Hello everyone,

I'm new to the use of mxArray in C++.
The use of these codes has been useful for me:

> mxArray* array_ptr=mxCreateDoubleMatrix(0,0,mxREAL);
> double* pr=mxCalloc(num_elems,sizeof(double));
> mxSetDimensions(array_ptr,ndims,new_dims);
> mxSetPr(array_ptr,pr);

> double* pr = mxGetPr(array_ptr);

The problem comes when I want to put directly an array into the dedicated zone of memory allocated by mxCalloc. I don't know how to pass directly an array to that zone and at the same time, to avoid the use of a c++ array to COPY the data to that zone through memcpy or a loop. Is there a way to request an mxArray to point directly to an array or to initialize directly that zone with an array of datas?

Fernando
From: James Tursa on
"Fernando " <gordo2(a)myutilities.cjb.net> wrote in message <h7rk43$ph5$1(a)fred.mathworks.com>...
> Hello everyone,
>
> I'm new to the use of mxArray in C++.
> The use of these codes has been useful for me:
>
> > mxArray* array_ptr=mxCreateDoubleMatrix(0,0,mxREAL);
> > double* pr=mxCalloc(num_elems,sizeof(double));
> > mxSetDimensions(array_ptr,ndims,new_dims);
> > mxSetPr(array_ptr,pr);
>
> > double* pr = mxGetPr(array_ptr);
>
> The problem comes when I want to put directly an array into the dedicated zone of memory allocated by mxCalloc. I don't know how to pass directly an array to that zone and at the same time, to avoid the use of a c++ array to COPY the data to that zone through memcpy or a loop. Is there a way to request an mxArray to point directly to an array or to initialize directly that zone with an array of datas?
>
> Fernando

I don't understand the question. Could you post some code, or at least an algorithm of sorts, of what you are trying to do? e.g., the phrase "... put directly an array into the dedicated zone of memory ..." is not clear to me.

James Tursa
From: Fernando on

Here's something I would like to do:

mxArray* array_ptr=mxCreateDoubleMatrix(0,0,mxREAL);
double pr[] = {5,8,9,6,4,7,9};
mxSetDimensions(array_ptr,ndims,new_dims);
mxSetPr(array_ptr,pr);

Of course, written that way, it does not work, but what I want to do is to introduce directly into array_ptr these data: {5,8,9,6,4,7,9}, written in an as simple way as {5,8,9,6,4,7,9}, instead of doing this:

double arraytest[] = {5,8,9,6,4,7,9};
mxArray* array_ptr=mxCreateDoubleMatrix(0,0,mxREAL);
double* pr=mxCalloc(num_elems,sizeof(double));
for (int i=0; i<7; i++) {*(pr+i)=arraytest[i];}
mxSetDimensions(array_ptr,ndims,new_dims);
mxSetPr(array_ptr,pr);

or this:

mxArray* array_ptr=mxCreateDoubleMatrix(ROWS,COLS,mxREAL);
double arraytest[] = {5,8,9,6,4,7,9};
double* pr = mxGetPr(array_ptr);
memcpy((void *)pr,(const void *)arraytest, ROWS*COLS*sizeof(double));
From: James Tursa on
"Fernando " <gordo2(a)myutilities.cjb.net> wrote in message <h7rmod$ii3$1(a)fred.mathworks.com>...
>
> Here's something I would like to do:
>
> mxArray* array_ptr=mxCreateDoubleMatrix(0,0,mxREAL);
> double pr[] = {5,8,9,6,4,7,9};
> mxSetDimensions(array_ptr,ndims,new_dims);
> mxSetPr(array_ptr,pr);
>
> Of course, written that way, it does not work, but what I want to do is to introduce directly into array_ptr these data: {5,8,9,6,4,7,9}, written in an as simple way as {5,8,9,6,4,7,9}, instead of doing this:
>
> double arraytest[] = {5,8,9,6,4,7,9};
> mxArray* array_ptr=mxCreateDoubleMatrix(0,0,mxREAL);
> double* pr=mxCalloc(num_elems,sizeof(double));
> for (int i=0; i<7; i++) {*(pr+i)=arraytest[i];}
> mxSetDimensions(array_ptr,ndims,new_dims);
> mxSetPr(array_ptr,pr);
>
> or this:
>
> mxArray* array_ptr=mxCreateDoubleMatrix(ROWS,COLS,mxREAL);
> double arraytest[] = {5,8,9,6,4,7,9};
> double* pr = mxGetPr(array_ptr);
> memcpy((void *)pr,(const void *)arraytest, ROWS*COLS*sizeof(double));

Sorry, it can't be done. What you are trying to do is essentially mix native C memory (fixed arrays or malloc stuff) with MATLAB memory. Anytime you do this and pass it back to MATLAB as part of an mxArray you will end up with seg faults etc because of invalid memory access and the like. The MATLAB memory manager knows nothing about this memory so when it tries to share the memory or free the memory things get messed up. You *must* copy any native C memory to mxCreate___ or mxMalloc etc memory if you want it to be part of an mxArray. There is no getting around this basic rule. So you will have to use a loop or memcpy etc to get the native C memory copied into the appropriate mxArray data areas.

James Tursa
 | 
Pages: 1
Prev: Uniformity Correction in MRI
Next: imregionalmax