Thread Subject: problems getting MATLAB data in C++ program

Subject: problems getting MATLAB data in C++ program

From: Paul Gordon

Date: 27 Dec, 2011 18:47:08

Message: 1 of 7

Hi I am using the MX functions to get read MATLAB data in C and have been able to read the MATLAB data for MATLAB arrays and structures (created using the keword struct) but am having issues reading the data from a MATLAB structure array like test(1).dataelement1, test(1).dataelement2. Does anyone know what mx method to use? Also I think MATLAB must store additional information about the structure array as if I set the variable that I bring over to C to the first element in MATLAB I can read only the first value of the structure array. Any ideas?

Subject: problems getting MATLAB data in C++ program

From: James Tursa

Date: 27 Dec, 2011 19:15:09

Message: 2 of 7

"Paul" wrote in message <jdd3rc$km1$1@newscl01ah.mathworks.com>...
> Hi I am using the MX functions to get read MATLAB data in C and have been able to read the MATLAB data for MATLAB arrays and structures (created using the keword struct) but am having issues reading the data from a MATLAB structure array like test(1).dataelement1, test(1).dataelement2. Does anyone know what mx method to use? Also I think MATLAB must store additional information about the structure array as if I set the variable that I bring over to C to the first element in MATLAB I can read only the first value of the structure array. Any ideas?

Use mxGetField. E.g., if you pass in the structure as the first argument:

mxArray *test_dataelement1, *test_dataelement2;
    :
test_dataelement1 = mxGetField(prhs[0],0,"dataelement1");
test_dataelement2 = mxGetField(prhs[0],0,"dataelement2");

James Tursa

Subject: problems getting MATLAB data in C++ program

From: Paul

Date: 27 Dec, 2011 22:16:08

Message: 3 of 7

"James Tursa" wrote in message <jdd5ft$pvl$1@newscl01ah.mathworks.com>...
> "Paul" wrote in message <jdd3rc$km1$1@newscl01ah.mathworks.com>...
> > Hi I am using the MX functions to get read MATLAB data in C and have been able to read the MATLAB data for MATLAB arrays and structures (created using the keword struct) but am having issues reading the data from a MATLAB structure array like test(1).dataelement1, test(1).dataelement2. Does anyone know what mx method to use? Also I think MATLAB must store additional information about the structure array as if I set the variable that I bring over to C to the first element in MATLAB I can read only the first value of the structure array. Any ideas?
>
> Use mxGetField. E.g., if you pass in the structure as the first argument:
>
> mxArray *test_dataelement1, *test_dataelement2;
> :
> test_dataelement1 = mxGetField(prhs[0],0,"dataelement1");
> test_dataelement2 = mxGetField(prhs[0],0,"dataelement2");
>
> James Tursa
Hi James, thanks for the response, I was able to add the code but was not sure how to extract the data from the mxArray pointer test_dataelement1? I tried
double firstvalue = test_dataelement1[0]; but get the error invalid use of mxArray tag?

Subject: problems getting MATLAB data in C++ program

From: Paul

Date: 27 Dec, 2011 23:30:09

Message: 4 of 7

"Paul" wrote in message <jddg38$kd$1@newscl01ah.mathworks.com>...
> "James Tursa" wrote in message <jdd5ft$pvl$1@newscl01ah.mathworks.com>...
> > "Paul" wrote in message <jdd3rc$km1$1@newscl01ah.mathworks.com>...
> > > Hi I am using the MX functions to get read MATLAB data in C and have been able to read the MATLAB data for MATLAB arrays and structures (created using the keword struct) but am having issues reading the data from a MATLAB structure array like test(1).dataelement1, test(1).dataelement2. Does anyone know what mx method to use? Also I think MATLAB must store additional information about the structure array as if I set the variable that I bring over to C to the first element in MATLAB I can read only the first value of the structure array. Any ideas?
> >
> > Use mxGetField. E.g., if you pass in the structure as the first argument:
> >
> > mxArray *test_dataelement1, *test_dataelement2;
> > :
> > test_dataelement1 = mxGetField(prhs[0],0,"dataelement1");
> > test_dataelement2 = mxGetField(prhs[0],0,"dataelement2");
> >
> > James Tursa
> Hi James, thanks for the response, I was able to add the code but was not sure how to extract the data from the mxArray pointer test_dataelement1? I tried
> double firstvalue = test_dataelement1[0]; but get the error invalid use of mxArray tag? I think I am not setting prhs correctly, seems as though it would need to be linked to the test_dataelement1 pointer?

Subject: problems getting MATLAB data in C++ program

From: James Tursa

Date: 28 Dec, 2011 07:33:08

Message: 5 of 7

"Paul" wrote in message <jddg38$kd$1@newscl01ah.mathworks.com>...
> "James Tursa" wrote in message <jdd5ft$pvl$1@newscl01ah.mathworks.com>...
> > "Paul" wrote in message <jdd3rc$km1$1@newscl01ah.mathworks.com>...
> > > Hi I am using the MX functions to get read MATLAB data in C and have been able to read the MATLAB data for MATLAB arrays and structures (created using the keword struct) but am having issues reading the data from a MATLAB structure array like test(1).dataelement1, test(1).dataelement2. Does anyone know what mx method to use? Also I think MATLAB must store additional information about the structure array as if I set the variable that I bring over to C to the first element in MATLAB I can read only the first value of the structure array. Any ideas?
> >
> > Use mxGetField. E.g., if you pass in the structure as the first argument:
> >
> > mxArray *test_dataelement1, *test_dataelement2;
> > :
> > test_dataelement1 = mxGetField(prhs[0],0,"dataelement1");
> > test_dataelement2 = mxGetField(prhs[0],0,"dataelement2");
> >
> > James Tursa
>
> Hi James, thanks for the response, I was able to add the code but was not sure how to extract the data from the mxArray pointer test_dataelement1? I tried
> double firstvalue = test_dataelement1[0]; but get the error invalid use of mxArray tag?

You need to extract the data pointer first, then dereference that. E.g., assuming that dataelement1 and 2 contain double class arrays, you would do this:

mxArray *test_dataelement1, *test_dataelement2;
double *pr1, *pr2;
    :
test_dataelement1 = mxGetField(prhs[0],0,"dataelement1");
if( test_dataelement1 == NULL ) {
    mexErrMsgTxt("insert error message here");
}
test_dataelement2 = mxGetField(prhs[0],0,"dataelement2");
if( test_dataelement2 == NULL ) {
    mexErrMsgTxt("insert error message here");
}
pr1 = mxGetPr(test_dataelement1);
pr2 = mxGetPr(test_dataelement2);
    :

pr1[0] is the first element, pr1[1] is the second element, etc.

If the underlying data is some other class, e.g. int32, then you would use mxGetData instead of mxGetPr and cast the resulting pointer appropriate to the underlying data.

James Tursa

Subject: problems getting MATLAB data in C++ program

From: Paul

Date: 28 Dec, 2011 17:18:07

Message: 6 of 7

"James Tursa" wrote in message <jdegnk$2k3$1@newscl01ah.mathworks.com>...
> "Paul" wrote in message <jddg38$kd$1@newscl01ah.mathworks.com>...
> > "James Tursa" wrote in message <jdd5ft$pvl$1@newscl01ah.mathworks.com>...
> > > "Paul" wrote in message <jdd3rc$km1$1@newscl01ah.mathworks.com>...
> > > > Hi I am using the MX functions to get read MATLAB data in C and have been able to read the MATLAB data for MATLAB arrays and structures (created using the keword struct) but am having issues reading the data from a MATLAB structure array like test(1).dataelement1, test(1).dataelement2. Does anyone know what mx method to use? Also I think MATLAB must store additional information about the structure array as if I set the variable that I bring over to C to the first element in MATLAB I can read only the first value of the structure array. Any ideas?
> > >
> > > Use mxGetField. E.g., if you pass in the structure as the first argument:
> > >
> > > mxArray *test_dataelement1, *test_dataelement2;
> > > :
> > > test_dataelement1 = mxGetField(prhs[0],0,"dataelement1");
> > > test_dataelement2 = mxGetField(prhs[0],0,"dataelement2");
> > >
> > > James Tursa
> >
> > Hi James, thanks for the response, I was able to add the code but was not sure how to extract the data from the mxArray pointer test_dataelement1? I tried
> > double firstvalue = test_dataelement1[0]; but get the error invalid use of mxArray tag?
>
> You need to extract the data pointer first, then dereference that. E.g., assuming that dataelement1 and 2 contain double class arrays, you would do this:
>
> mxArray *test_dataelement1, *test_dataelement2;
> double *pr1, *pr2;
> :
> test_dataelement1 = mxGetField(prhs[0],0,"dataelement1");
> if( test_dataelement1 == NULL ) {
> mexErrMsgTxt("insert error message here");
> }
> test_dataelement2 = mxGetField(prhs[0],0,"dataelement2");
> if( test_dataelement2 == NULL ) {
> mexErrMsgTxt("insert error message here");
> }
> pr1 = mxGetPr(test_dataelement1);
> pr2 = mxGetPr(test_dataelement2);
> :
>
> pr1[0] is the first element, pr1[1] is the second element, etc.
>
> If the underlying data is some other class, e.g. int32, then you would use mxGetData instead of mxGetPr and cast the resulting pointer appropriate to the underlying data.
>
> James Tursa
Hi James, thanks for the additional information. I am getting the following compiler error
on line test_dataelement1=mxGetField(prhs[0],0,"dataelement1");
prhs was not declared in this scope. I tried adding const mxArray *prhs[2]; and it does get rid of the compiler error on that line but then I get a runtime error on that line. Also wondering if I am missing an include file as when I added mexErrMsgTxt I get the error was not declared in this scope. I have been able to read data from a matlab array dereferencing the pointer as you described.

Subject: problems getting MATLAB data in C++ program

From: James Tursa

Date: 28 Dec, 2011 21:12:08

Message: 7 of 7

"Paul" wrote in message <jdfj0f$fm0$1@newscl01ah.mathworks.com>...
>
> Hi James, thanks for the additional information. I am getting the following compiler error
> on line test_dataelement1=mxGetField(prhs[0],0,"dataelement1");
> prhs was not declared in this scope. I tried adding const mxArray *prhs[2]; and it does get rid of the compiler error on that line but then I get a runtime error on that line. Also wondering if I am missing an include file as when I added mexErrMsgTxt I get the error was not declared in this scope. I have been able to read data from a matlab array dereferencing the pointer as you described.

Please post your entire code so I can look at everything in context.

James Tursa

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com