From: Eyup Cinar on
Hello,

I am trying to send data from C++ using Matlab's engine libraries.

This might be fairly a simple question but I couldn't figure it out. I wonder how an array of double may be converted into an mxn Matlab array when it's sent to the Matlab's workspace.

Is this automatically handled by Engine libraries or what should I do ? For example, I have an array of 14*128=1792 in size which is an array of double type in C++ and I want to see an array in the matlab's workspace in the size of 12 by 128 when I use the function engPutVariable . Do you think the following would be appropriate and not cause any problem ?

data being the double array in C++ as I mentioned

mtlbDataArray=mxCreateDoubleMatrix(14,128,mxREAL);

data=mxGetPr(mtlbDataArray);

engPutVariable(ep,"myArray",mtlbDataArray);

From: James Tursa on
"Eyup Cinar" <exc8020(a)rit.edu> wrote in message <hqdocl$2j3$1(a)fred.mathworks.com>...
> Hello,
>
> I am trying to send data from C++ using Matlab's engine libraries.
>
> This might be fairly a simple question but I couldn't figure it out. I wonder how an array of double may be converted into an mxn Matlab array when it's sent to the Matlab's workspace.
>
> Is this automatically handled by Engine libraries or what should I do ? For example, I have an array of 14*128=1792 in size which is an array of double type in C++ and I want to see an array in the matlab's workspace in the size of 12 by 128 when I use the function engPutVariable . Do you think the following would be appropriate and not cause any problem ?
>
> data being the double array in C++ as I mentioned
>
> mtlbDataArray=mxCreateDoubleMatrix(14,128,mxREAL);
>
> data=mxGetPr(mtlbDataArray);
>
> engPutVariable(ep,"myArray",mtlbDataArray);
>

You didn't put any values into the data area of mtlbDataArray. Once you get the pointer via mxGetPr you need to copy values into the array. e.g.,

double x[1792];
double *data;
int i;
mxArray *mtlbDataArray;
// code to fill in values of x
// code to open engine
mtlbDataArray = mxCreateDoubleMatrix(14, 128, mxREAL);
if( mtlbDataArray == NULL ) {
// handle the memory allocation error here
}
data = mxGetPr(mtlbDataArray);
for( i=0; i<1792; i++ ){
data[i] = x[i];
}
engPutVariable(ep, "myArray", mtlbDataArray);
mxDestroyArray(mtlbDataArray);

This assumes that the data on the C side is in column order in memory.

James Tursa
From: Eyup Cinar on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hqdqcd$2v9$1(a)fred.mathworks.com>...
> "Eyup Cinar" <exc8020(a)rit.edu> wrote in message <hqdocl$2j3$1(a)fred.mathworks.com>...
> > Hello,
> >
> > I am trying to send data from C++ using Matlab's engine libraries.
> >
> > This might be fairly a simple question but I couldn't figure it out. I wonder how an array of double may be converted into an mxn Matlab array when it's sent to the Matlab's workspace.
> >
> > Is this automatically handled by Engine libraries or what should I do ? For example, I have an array of 14*128=1792 in size which is an array of double type in C++ and I want to see an array in the matlab's workspace in the size of 12 by 128 when I use the function engPutVariable . Do you think the following would be appropriate and not cause any problem ?
> >
> > data being the double array in C++ as I mentioned
> >
> > mtlbDataArray=mxCreateDoubleMatrix(14,128,mxREAL);
> >
> > data=mxGetPr(mtlbDataArray);
> >
> > engPutVariable(ep,"myArray",mtlbDataArray);
> >
>
> You didn't put any values into the data area of mtlbDataArray. Once you get the pointer via mxGetPr you need to copy values into the array. e.g.,
>
> double x[1792];
> double *data;
> int i;
> mxArray *mtlbDataArray;
> // code to fill in values of x
> // code to open engine
> mtlbDataArray = mxCreateDoubleMatrix(14, 128, mxREAL);
> if( mtlbDataArray == NULL ) {
> // handle the memory allocation error here
> }
> data = mxGetPr(mtlbDataArray);
> for( i=0; i<1792; i++ ){
> data[i] = x[i];
> }
> engPutVariable(ep, "myArray", mtlbDataArray);
> mxDestroyArray(mtlbDataArray);
>
> This assumes that the data on the C side is in column order in memory.
>
> James Tursa

Hi James,
Appreciated for your quick response!

Sorry I forgot to mention that data array is filled with a command that grabs the data from a data acquisition device such as EE_DataGet(data)

I have just realized when I was debugging that the data on the C side is copied into the mxArray in column order. That is, for example as a very simple case : if I say my double type C array contains the data such as :

data={1,2,3,4,5,6,7,8};

I can see an mxArray in the workspace with a size of 2 by 4 however the elements are in the order such as (in the matlab syntax)

mtlbDataArray=[1;2,3;4,5;6,7;8]

but I need the array content to be created as mtlbDataArray=[1,2,3,4; 5,6,7,8] that is copied row by row, can you offer me a way to avoid this ?

Thanks again
From: Eyup Cinar on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hqe63c$1lg$1(a)fred.mathworks.com>...
> "Eyup Cinar" <exc8020(a)rit.edu> wrote in message <hqds8d$tf$1(a)fred.mathworks.com>...
> >
> > I have just realized when I was debugging that the data on the C side is copied into the mxArray in column order. That is, for example as a very simple case : if I say my double type C array contains the data such as :
> >
> > data={1,2,3,4,5,6,7,8};
> >
> > I can see an mxArray in the workspace with a size of 2 by 4 however the elements are in the order such as (in the matlab syntax)
> >
> > mtlbDataArray=[1;2,3;4,5;6,7;8]
> >
> > but I need the array content to be created as mtlbDataArray=[1,2,3,4; 5,6,7,8] that is copied row by row, can you offer me a way to avoid this ?
>
> The easiest thing to do is transpose it after you get it into the engine. Just use engEvalString(ep, "myArray = myArray.';");
>
> You *could* write code on the C side to do this, but why bother since it is so easy to do on the MATLAB side?
>
> James Tursa

Thanks James I got figured it out after I posted my previous message ! sorry about the post.
From: James Tursa on
"Eyup Cinar" <exc8020(a)rit.edu> wrote in message <hqds8d$tf$1(a)fred.mathworks.com>...
>
> I have just realized when I was debugging that the data on the C side is copied into the mxArray in column order. That is, for example as a very simple case : if I say my double type C array contains the data such as :
>
> data={1,2,3,4,5,6,7,8};
>
> I can see an mxArray in the workspace with a size of 2 by 4 however the elements are in the order such as (in the matlab syntax)
>
> mtlbDataArray=[1;2,3;4,5;6,7;8]
>
> but I need the array content to be created as mtlbDataArray=[1,2,3,4; 5,6,7,8] that is copied row by row, can you offer me a way to avoid this ?

The easiest thing to do is transpose it after you get it into the engine. Just use engEvalString(ep, "myArray = myArray.';");

You *could* write code on the C side to do this, but why bother since it is so easy to do on the MATLAB side?

James Tursa