Thread Subject: c++ struct in mxstruct

Subject: c++ struct in mxstruct

From: Sergio

Date: 15 Mar, 2010 08:49:05

Message: 1 of 7

Hi all,

i need to pass an array of structures declared in c++ to matlab. Is there a way to do it without passing value one by one converting them to double or else?

typedef struct
{
double lat;
double long;
char name[11] ;
} pos;


const char* fields[] = {"lat", "long", "name"};
mwArray a(1, 10, 3, fields);

for setting values, i generally use the following code
double flag = {10.0};
mwArray temp(1,1,mxDOUBLE_CLASS);
float dato;
temp.SetData(&flag,1);
a.Get("lat",1,10).Set(temp);

is there a faster way to do it? in addition, if someone can help me to set char values, cause similar method seems to fail even if i use SetCharData instead of Set.
Thx for help

Subject: c++ struct in mxstruct

From: Rune Allnor

Date: 15 Mar, 2010 09:16:32

Message: 2 of 7

On 15 Mar, 09:49, "Sergio " <serjo...@inwind.it> wrote:
> Hi all,
>
> i need to pass an array of structures declared in c++ to matlab. Is there a way to do it without passing value one by one converting them to double or else?

There are some demos with matlab on how to declare and
access fields in matlab structs through MEX. Have a look
at the c and cpp files that come with matlab.

> typedef struct
> {
>         double lat;
>         double long;
>         char name[11] ;
>         } pos;
>
> const char* fields[] = {"lat", "long", "name"};
> mwArray a(1, 10, 3, fields);
>
> for setting values, i generally use the following code
> double flag = {10.0};
> mwArray temp(1,1,mxDOUBLE_CLASS);
> float dato;
> temp.SetData(&flag,1);
> a.Get("lat",1,10).Set(temp);
>
> is there a faster way to do it?

"Fast" has nothing to do with this. No need to do these
kinds of things at all, unless they are "correct".

Rune

Subject: c++ struct in mxstruct

From: Sergio

Date: 15 Mar, 2010 09:46:05

Message: 3 of 7

Rune Allnor <allnor@tele.ntnu.no> wrote in message <b63995a3-bd45-4d16-bdfb-00b51ad6a8f2@z4g2000yqa.googlegroups.com>...
> On 15 Mar, 09:49, "Sergio " <serjo...@inwind.it> wrote:
> > Hi all,
> >
> > i need to pass an array of structures declared in c++ to matlab. Is there a way to do it without passing value one by one converting them to double or else?
>
> There are some demos with matlab on how to declare and
> access fields in matlab structs through MEX. Have a look
> at the c and cpp files that come with matlab.
>
> > typedef struct
> > {
> >         double lat;
> >         double long;
> >         char name[11] ;
> >         } pos;
> >
> > const char* fields[] = {"lat", "long", "name"};
> > mwArray a(1, 10, 3, fields);
> >
> > for setting values, i generally use the following code
> > double flag = {10.0};
> > mwArray temp(1,1,mxDOUBLE_CLASS);
> > float dato;
> > temp.SetData(&flag,1);
> > a.Get("lat",1,10).Set(temp);
> >
> > is there a faster way to do it?
>
> "Fast" has nothing to do with this. No need to do these
> kinds of things at all, unless they are "correct".
>
> Rune

Hi Rune,
thx for replying.

i read mex examples and tried to pass the structure even with a mex file, i can correctly create a variable in matlab workspace but i need to access that data through a matlab compiled dll. The problem is that when i call that dll, probably it opens another instance of matlab and data are nonexistent.

Subject: c++ struct in mxstruct

From: Ashish Uthama

Date: 15 Mar, 2010 12:47:47

Message: 4 of 7

On Mon, 15 Mar 2010 05:46:05 -0400, Sergio <serjossj@inwind.it> wrote:

> Rune Allnor <allnor@tele.ntnu.no> wrote in message
> <b63995a3-bd45-4d16-bdfb-00b51ad6a8f2@z4g2000yqa.googlegroups.com>...
>> On 15 Mar, 09:49, "Sergio " <serjo...@inwind.it> wrote:
>> > Hi all,
>> >
>> > i need to pass an array of structures declared in c++ to matlab. Is
>> there a way to do it without passing value one by one converting them
>> to double or else?
>> There are some demos with matlab on how to declare and
>> access fields in matlab structs through MEX. Have a look
>> at the c and cpp files that come with matlab.
>> > typedef struct
>> > {
>> > double lat;
>> > double long;
>> > char name[11] ;
>> > } pos;
>> >
>> > const char* fields[] = {"lat", "long", "name"};
>> > mwArray a(1, 10, 3, fields);
>> >
>> > for setting values, i generally use the following code
>> > double flag = {10.0};
>> > mwArray temp(1,1,mxDOUBLE_CLASS);
>> > float dato;
>> > temp.SetData(&flag,1);
>> > a.Get("lat",1,10).Set(temp);
>> >
>> > is there a faster way to do it?
>> "Fast" has nothing to do with this. No need to do these
>> kinds of things at all, unless they are "correct".
>> Rune
>
> Hi Rune,
> thx for replying.
>
> i read mex examples and tried to pass the structure even with a mex
> file, i can correctly create a variable in matlab workspace but i need
> to access that data through a matlab compiled dll. The problem is that
> when i call that dll, probably it opens another instance of matlab and
> data are nonexistent.

Serjo,
Could you explain your setup some more?

It is not clear if you are trying to pass a structure from C++ Mex file to
MATLAB. Or, if you have a C++ application which uses a MATLAB code
compiled into a DLL. I believe its the latter, in which case, yes, the DLL
will create its own instance of 'MATLAB' (Look up MCR in the doc). It will
not have access to your interactive MATLAB session's workspace.

Note that mxArray are C/C++ source code for Mex files for use in a MATLAB
session, mwArray are for C++ source code for drivers to use MATLAB
compiled DLLs

Subject: c++ struct in mxstruct

From: Sergio

Date: 17 Mar, 2010 08:47:22

Message: 5 of 7

"Ashish Uthama" <first.last@mathworks.com> wrote in message <
>
> Serjo,
> Could you explain your setup some more?
>
> It is not clear if you are trying to pass a structure from C++ Mex file to
> MATLAB. Or, if you have a C++ application which uses a MATLAB code
> compiled into a DLL. I believe its the latter, in which case, yes, the DLL
> will create its own instance of 'MATLAB' (Look up MCR in the doc). It will
> not have access to your interactive MATLAB session's workspace.
>
> Note that mxArray are C/C++ source code for Mex files for use in a MATLAB
> session, mwArray are for C++ source code for drivers to use MATLAB
> compiled DLLs
>
>
>

Hi,
I've a MATLAB code compiled in a dll so i need to convert that struct as input argument.
I managed to create that struct directly in an mwArray but, i'm having some problem with filling it. Here is the code:

const char* fields[] = {"latitude", "longitude", "altitude"};
mwArray entitylist(1, Total_entities, 3, fields)
mwArray temp(1,1,mxDOUBLE_CLASS);
double a;

for (int i_list = 1; i_list<=Total_entities; i_list++)
       {
a = latitude;
temp.SetData(&a,1);
        entitylist.Get("latitude",1,i_list).Set(temp);
        }
while filling mwArray entitylist, just last value is loaded even if column index is specified by i_list. (ie, if i've Total entities equal to 3, just last latitude value is loaded into entitylist.latitude and it fills all 3 columns.

Am i doing something wrong?

Thx

Subject: c++ struct in mxstruct

From: Ashish Uthama

Date: 17 Mar, 2010 19:19:53

Message: 6 of 7

On Wed, 17 Mar 2010 04:47:22 -0400, Sergio <serjossj@inwind.it> wrote:

> "Ashish Uthama" <first.last@mathworks.com> wrote in message <
>> Serjo,
>> Could you explain your setup some more?
>> It is not clear if you are trying to pass a structure from C++ Mex
>> file to MATLAB. Or, if you have a C++ application which uses a MATLAB
>> code compiled into a DLL. I believe its the latter, in which case,
>> yes, the DLL will create its own instance of 'MATLAB' (Look up MCR in
>> the doc). It will not have access to your interactive MATLAB session's
>> workspace.
>> Note that mxArray are C/C++ source code for Mex files for use in a
>> MATLAB session, mwArray are for C++ source code for drivers to use
>> MATLAB compiled DLLs
>>
>
> Hi,
> I've a MATLAB code compiled in a dll so i need to convert that struct as
> input argument.
> I managed to create that struct directly in an mwArray but, i'm having
> some problem with filling it. Here is the code:
>
> const char* fields[] = {"latitude", "longitude", "altitude"};
> mwArray entitylist(1, Total_entities, 3, fields)
> mwArray temp(1,1,mxDOUBLE_CLASS);
> double a;
>
> for (int i_list = 1; i_list<=Total_entities; i_list++)
> {
> a = latitude;
> temp.SetData(&a,1);
> entitylist.Get("latitude",1,i_list).Set(temp);
> }
> while filling mwArray entitylist, just last value is loaded even if
> column index is specified by i_list. (ie, if i've Total entities equal
> to 3, just last latitude value is loaded into entitylist.latitude and it
> fills all 3 columns. Am i doing something wrong?
>
> Thx

Your code appears to be doing exactly what you observe.
I dont see the value of 'a' changing inside the loop, I would expect
entitylist.latitude to have same valued elements.

Subject: c++ struct in mxstruct

From: Sergio

Date: 18 Mar, 2010 09:59:04

Message: 7 of 7

> > const char* fields[] = {"latitude", "longitude", "altitude"};
> > mwArray entitylist(1, Total_entities, 3, fields)
> > mwArray temp(1,1,mxDOUBLE_CLASS);
> > double a;
> >
> > for (int i_list = 1; i_list<=Total_entities; i_list++)
> > {
> > a = latitude;
> > temp.SetData(&a,1);
> > entitylist.Get("latitude",1,i_list).Set(temp);
> > }

> Your code appears to be doing exactly what you observe.
> I dont see the value of 'a' changing inside the loop, I would expect
> entitylist.latitude to have same valued elements.


but i_list change within for cycle and, with each for cycle i've a different latitude value.
so, i would set a different value of latitude (this is not an array but change accordingly to entity, outside for cycle).
So, i thought i might fill it just by chosing row and column index.
btw, i found a mode to handle it:

a = latitude;
temp.Get(1,i_list).SetData(&a,1);
entitylist.Get("latitude",1,i_list).Set(temp.Get(1,i_list));

in this way it seems struct is corerctly loaded.

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
mxstruct c struct Sergio 15 Mar, 2010 04:54:06
rssFeed for this Thread

Contact us at files@mathworks.com