From: Sergio on
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
From: Rune Allnor on
On 15 Mar, 09:49, "Sergio " <serjo...(a)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
From: Sergio on
Rune Allnor <allnor(a)tele.ntnu.no> wrote in message <b63995a3-bd45-4d16-bdfb-00b51ad6a8f2(a)z4g2000yqa.googlegroups.com>...
> On 15 Mar, 09:49, "Sergio " <serjo...(a)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.
From: Ashish Uthama on
On Mon, 15 Mar 2010 05:46:05 -0400, Sergio <serjossj(a)inwind.it> wrote:

> Rune Allnor <allnor(a)tele.ntnu.no> wrote in message
> <b63995a3-bd45-4d16-bdfb-00b51ad6a8f2(a)z4g2000yqa.googlegroups.com>...
>> On 15 Mar, 09:49, "Sergio " <serjo...(a)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




From: Sergio on
"Ashish Uthama" <first.last(a)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
 |  Next  |  Last
Pages: 1 2
Prev: esperanc
Next: mxstruct filling