Thread Subject: C++ and Matlab

Subject: C++ and Matlab

From: Izzy

Date: 4 Nov, 2009 14:07:48

Message: 1 of 8

Hi,

If i create a singleton object in c++ code and send it into matlab and
use it there, does the singleton share the same memory or are there 2
singelton around, one in c++ memory space and one in matlab memory
space?

Thanks

Subject: C++ and Matlab

From: Sebastiaan

Date: 4 Nov, 2009 14:14:02

Message: 2 of 8

Izzy <iskandarprins@gmail.com> wrote in message <79efd49c-346d-40b2-a663-67f754b7b8da@g23g2000yqh.googlegroups.com>...
> Hi,
>
> If i create a singleton object in c++ code and send it into matlab and
> use it there, does the singleton share the same memory or are there 2
> singelton around, one in c++ memory space and one in matlab memory
> space?
>
> Thanks
I am not sure if I fully understand...

If you have a C++ subroutine which is called by Matlab, you share the memory (if you use it well: Matlab will give your C++ routine a pointer).

I am not sure how it works if you are calling Matlab from a C++ program...

Sebastiaan

Subject: C++ and Matlab

From: James Tursa

Date: 4 Nov, 2009 14:14:02

Message: 3 of 8

Izzy <iskandarprins@gmail.com> wrote in message <79efd49c-346d-40b2-a663-67f754b7b8da@g23g2000yqh.googlegroups.com>...
> Hi,
>
> If i create a singleton object in c++ code and send it into matlab and
> use it there, does the singleton share the same memory or are there 2
> singelton around, one in c++ memory space and one in matlab memory
> space?

Show us your code. How are you sending it to MATLAB?

James Tursa

Subject: C++ and Matlab

From: Izzy

Date: 5 Nov, 2009 12:55:45

Message: 4 of 8

On Nov 4, 3:14 pm, "James Tursa"
<aclassyguy_with_a_k_not_...@hotmail.com> wrote:
> Izzy <iskandarpr...@gmail.com> wrote in message <79efd49c-346d-40b2-a663-67f754b7b...@g23g2000yqh.googlegroups.com>...
> > Hi,
>
> > If i create a singleton object inc++code and send it into matlab and
> > use it there, does the singleton share the same memory or are there 2
> > singelton around, one inc++memory space and one in matlab memory
> > space?
>
> Show us your code. How are you sending it to MATLAB?
>
> James Tursa

No code avalaible yet, since i'm just starting with this c++/matlab
thing. Basically I have a singleton class in c++ land, which also
needs to be used in matlab land. I was wondering if both lands share
the same memory if i would pass the singleton into matlab and do some
modification on the singleton, if this singleton would still be the
same object.

Subject: C++ and Matlab

From: Rune Allnor

Date: 5 Nov, 2009 14:18:03

Message: 5 of 8

On 5 Nov, 13:55, Izzy <iskandarpr...@gmail.com> wrote:
> On Nov 4, 3:14 pm, "James Tursa"
>
> <aclassyguy_with_a_k_not_...@hotmail.com> wrote:
> > Izzy <iskandarpr...@gmail.com> wrote in message <79efd49c-346d-40b2-a663-67f754b7b...@g23g2000yqh.googlegroups.com>...
> > > Hi,
>
> > > If i create a singleton object inc++code and send it into matlab and
> > > use it there, does the singleton share the same memory or are there 2
> > > singelton around, one inc++memory space and one in matlab memory
> > > space?
>
> > Show us your code. How are you sending it to MATLAB?
>
> > James Tursa
>
> No code avalaible yet, since i'm just starting with this c++/matlab
> thing. Basically I have a singleton class in c++ land, which also
> needs to be used in matlab land. I was wondering if both lands share
> the same memory if i would pass the singleton into matlab and do some
> modification on the singleton, if this singleton would still be the
> same object.

How are you proposing to do this?

You can make C++ and matlab interact in two ways:

1) Call C++ from matlab in the form of MEXfiles
2) Call the matlab engine from a C++ executable

In case 1 your C++ singleton will have to be declared and
initialized somewhere near the entry point of your C++
MEX routine, and be available to your C++ code. However,
its scope - and thus lifetime - will end near the return
point of your MEX routine. So there is no reason to expect
the singleton to be available from matlab scope, or to
stay available between multiple calls to the MEX routine.

In case 2 you would ideally want to pass the singleton
from C++ into matlab scope, to become accessible to matlab.
That can not be done. The best you can do is to pass variables
from the singleton through the engine gateway, and read
modified data back.

Rune

Subject: C++ and Matlab

From: James Tursa

Date: 5 Nov, 2009 14:46:03

Message: 6 of 8

Izzy <iskandarprins@gmail.com> wrote in message <ab897769-b6b9-49a5-8926-79681bcec4c1@g23g2000yqh.googlegroups.com>...
> On Nov 4, 3:14?pm, "James Tursa"
> <aclassyguy_with_a_k_not_...@hotmail.com> wrote:
> > Izzy <iskandarpr...@gmail.com> wrote in message <79efd49c-346d-40b2-a663-67f754b7b...@g23g2000yqh.googlegroups.com>...
> > > Hi,
> >
> > > If i create a singleton object inc++code and send it into matlab and
> > > use it there, does the singleton share the same memory or are there 2
> > > singelton around, one inc++memory space and one in matlab memory
> > > space?
> >
> > Show us your code. How are you sending it to MATLAB?
> >
> > James Tursa
>
> No code avalaible yet, since i'm just starting with this c++/matlab
> thing. Basically I have a singleton class in c++ land, which also
> needs to be used in matlab land. I was wondering if both lands share
> the same memory if i would pass the singleton into matlab and do some
> modification on the singleton, if this singleton would still be the
> same object.

In general, then, the answer is no. Your singleton could live inside the mex routine, and if you put it at the top level (outside the mexFunction) it will maintain its state across multiple mex calls. But to pass it back into the MATLAB workspace you will have to somehow turn it into an equivalent mxArray. This *copy* would be passed back into the MATLAB workspace. Changing the state of this *copy* in the MATLAB workspace will not affect the original singleton in your mex C++ routine.

James Tursa

Subject: C++ and Matlab

From: Izzy

Date: 6 Nov, 2009 08:22:46

Message: 7 of 8

On Nov 5, 3:46 pm, "James Tursa"
<aclassyguy_with_a_k_not_...@hotmail.com> wrote:
> Izzy <iskandarpr...@gmail.com> wrote in message <ab897769-b6b9-49a5-8926-79681bcec...@g23g2000yqh.googlegroups.com>...
> > On Nov 4, 3:14?pm, "James Tursa"
> > <aclassyguy_with_a_k_not_...@hotmail.com> wrote:
> > > Izzy <iskandarpr...@gmail.com> wrote in message <79efd49c-346d-40b2-a663-67f754b7b...@g23g2000yqh.googlegroups.com>...
> > > > Hi,
>
> > > > If i create a singleton object inc++code and send it into matlab and
> > > > use it there, does the singleton share the same memory or are there 2
> > > > singelton around, one inc++memory space and one in matlab memory
> > > > space?
>
> > > Show us your code. How are you sending it to MATLAB?
>
> > > James Tursa
>
> > No code avalaible yet, since i'm just starting with thisc++/matlab
> > thing. Basically I have a singleton class inc++land, which also
> > needs to be used in matlab land. I was wondering if both lands share
> > the same memory if i would pass the singleton into matlab and do some
> > modification on the singleton, if this singleton would still be the
> > same object.
>
> In general, then, the answer is no. Your singleton could live inside the mex routine, and if you put it at the top level (outside the mexFunction) it will maintain its state across multiple mex calls. But to pass it back into the MATLAB workspace you will have to somehow turn it into an equivalent mxArray. This *copy* would be passed back into the MATLAB workspace. Changing the state of this *copy* in the MATLAB workspace will not affect the original singleton in your mexC++routine.
>
> James Tursa- Hide quoted text -
>
> - Show quoted text -

Aha, thanks. So basically whenever I send in data to and from matlab a
copy is made thus is invalidates the usage of a singleton.

Subject: C++ and Matlab

From: James Tursa

Date: 6 Nov, 2009 16:14:02

Message: 8 of 8

Izzy <iskandarprins@gmail.com> wrote in message <7fa345d3-dbfa-4afd-b79d-4ffe72c6192c@g23g2000yqh.googlegroups.com>...
>
> Aha, thanks. So basically whenever I send in data to and from matlab a
> copy is made thus is invalidates the usage of a singleton.

From a practical standpoint, yes. From a technical standpoint, you *could* create your singleton using mxMalloc etc memory allocation functions and make that memory part of an mxArray that was returned to MATLAB. MATLAB will not actually copy the data area of the variable you return. But once you *change* the data area on the MATLAB side MATLAB will create a new data area for the changed results and break the connection between the MATLAB variable and your singleton. So it amounts to the same thing.

To get the functionality of your singleton on the MATLAB side you would have to resort to calling the mex file for data access and data setting. I.e., have your singleton at the top level so it maintains its state across multiple mex calls. Then when you want the value, call the mex routine with a special input (e.g., a string 'get') that will cause the mex function to return a copy of the singleton as an mxArray. If you want to change the state of the singleton, then create the changed state on the MATLAB side and call the mex routine with this new state and another special input (e.g., a string 'set') that sets the singleton value. Not a real clean solution, but you could get it to work.

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