Thread Subject: Create C++ Class in Matlab

Subject: Create C++ Class in Matlab

From: Stiphu

Date: 15 Oct, 2009 12:44:04

Message: 1 of 3

Hi there

I would like to call a C++ class from Matlab.

Example:

class testClass
   testClass() // Constructor
   getValues() // Function

Now I would like to create this class in Matlab

testClass myClass();

and then call the function

for i = 1:10
   x(i) = getValues;
end

and then call the destructor at the end of course, like you do it in C++. Is this possible in Matlab?

Subject: Create C++ Class in Matlab

From: Rune Allnor

Date: 15 Oct, 2009 13:17:29

Message: 2 of 3

On 15 Okt, 14:44, "Stiphu " <schm...@pyl.unibe.ch> wrote:
> Hi there
>
> I would like to call a C++ class from Matlab.
>
> Example:
>
> class testClass
>    testClass() // Constructor
>    getValues() // Function
>
> Now I would like to create this class in Matlab
>
> testClass myClass();
>
> and then call the function
>
> for i = 1:10
>    x(i) = getValues;
> end
>
> and then call the destructor at the end of course, like you do it in C++. Is this possible in Matlab?

You can interface matlab with C++, if that's what you ask.
You can do this via the MEX interface. The C++ object are
constructed at the start of the MEX routine, handled in the
body, and destructed at the end of the routine.

However, it is, to my knowledge, not possible to have C++
objects exist statically in the matlab environment. So you
can not construct a C++ object to access from the matlab
workspace.

Rune

Subject: Create C++ Class in Matlab

From: Stiphu

Date: 15 Oct, 2009 14:53:04

Message: 3 of 3

Instead of creating a class in Matlab, I want to check the state of a button dynamically from a c++ function. The c++ function looks like that:

// Definitions
mxArray *plhs[1];
mxArray *parameter = mxCreateString("Value");
prhs[0] = hButton; // Handle to the button
prhs[1] = parameter; // property object

bool running = true;

while(running ){
   ...
   mexCallMATLAB(1,plhs,2,prhs,"get");
   running = (bool)mxGetLogicals(plhs[0]);
}

hButton is the handle to a toggle button. The problem is: I can toggle the button, but the value I read in the mex function is always "true". Looks like a kind of a master/slave problem. I guess the GUI value is not actuall, as long as the c++ function is running. Is that right? Is there a possibility to interface to the c++ function while it's running?

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