Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Using classes in a compiled library

Subject: Using classes in a compiled library

From: Andrew Shepherd

Date: 16 Mar, 2009 11:05:18

Message: 1 of 5

Using the compiler toolbox, it is possible to create a DLL from which you can call MATLAB functions that you wrote.

What about class methods? It seems that if you compile a .m file containing a classdef, you can call the constructor, but that's it. You can't call any of the methods.

Am I missing something, or do I have to resign myself to not using the object oriented features if I want to write components in compiled libraries?

Subject: Using classes in a compiled library

From: Steven Lord

Date: 16 Mar, 2009 13:39:51

Message: 2 of 5


"Andrew Shepherd" <shepherd_a@htotmail.dontspamme.com> wrote in message
news:gplbpe$f3l$1@fred.mathworks.com...
> Using the compiler toolbox, it is possible to create a DLL from which you
> can call MATLAB functions that you wrote.
>
> What about class methods? It seems that if you compile a .m file
> containing a classdef, you can call the constructor, but that's it. You
> can't call any of the methods.
>
> Am I missing something, or do I have to resign myself to not using the
> object oriented features if I want to write components in compiled
> libraries?

I was able to define a class with a method, create an M-file function that
constructs an object of that class and calls the method, compile the M-file
and execute it without any problems in the R2009a release.

1) What versions of MATLAB and MATLAB Compiler are you using? You can find
this information using the VER function.

2) Do you receive an error message either during the compilation or (if
compilation succeeded) during execution?

3) How are you determining that the methods you tried to call are not being
called? [Are they supposed to display a message and they don't, are they
supposed to write to a file and they don't, do they give the wrong answer,
etc.]

4) Is your class defined all in one file or in multiple files? If the
latter, are you including them in the compilation process?

5) Do you have a _small_ example that demonstrates the problem?

--
Steve Lord
slord@mathworks.com

Subject: Using classes in a compiled library

From: Andrew Shepherd

Date: 16 Mar, 2009 22:56:01

Message: 3 of 5

"Steven Lord" <slord@mathworks.com> wrote in message <gplkqu$t89$1@fred.mathworks.com>...
>
> "Andrew Shepherd" <shepherd_a@htotmail.dontspamme.com> wrote in message
> news:gplbpe$f3l$1@fred.mathworks.com...
> > Using the compiler toolbox, it is possible to create a DLL from which you
> > can call MATLAB functions that you wrote.
> >
> > What about class methods? It seems that if you compile a .m file
> > containing a classdef, you can call the constructor, but that's it. You
> > can't call any of the methods.
> >
> > Am I missing something, or do I have to resign myself to not using the
> > object oriented features if I want to write components in compiled
> > libraries?
>
> I was able to define a class with a method, create an M-file function that
> constructs an object of that class and calls the method, compile the M-file
> and execute it without any problems in the R2009a release.
>
> 1) What versions of MATLAB and MATLAB Compiler are you using? You can find
> this information using the VER function.
>
> 2) Do you receive an error message either during the compilation or (if
> compilation succeeded) during execution?
>
> 3) How are you determining that the methods you tried to call are not being
> called? [Are they supposed to display a message and they don't, are they
> supposed to write to a file and they don't, do they give the wrong answer,
> etc.]
>
> 4) Is your class defined all in one file or in multiple files? If the
> latter, are you including them in the compilation process?
>
> 5) Do you have a _small_ example that demonstrates the problem?
>
> --
> Steve Lord
> slord@mathworks.com
>

I'm ussing MATLAB r2008b.

I want to create a .DLL file that contains the class functionality. Then, via C++, I want to create an instance of this MATLAB class, and then invoke a method on this class.

I am making the assumption that I am unable to call these methods, because these methods simply do not appear in the generated .H file.

The class I've compiled is below.

I can call the sortdisplay constructor, but the functions "settitle" and "update" are absent. Using Visual Studio's "dumpbin" utility, these are the functions exported from the DLL.


    ordinal hint RVA name

          1 0 000018E0 ?sortdisplay@@YAXHAAVmwArray@@ABV1@@Z
          2 1 000014F0 SortDisplayGetMcrID
          3 2 000014B0 SortDisplayInitialize
          4 3 00001440 SortDisplayInitializeWithHandlers
          5 4 00001500 SortDisplayPrintStackTrace
          6 5 000014D0 SortDisplayTerminate
          7 6 00001570 mlxSortdisplay



Here is the class I compiled.

classdef sortdisplay < handle
    properties (SetAccess = private)
        AxesHandle
        FigureHandle
    end
    methods
        function s = sortdisplay(data)
            % s.FigureHandle = figure();
            disp('Inside the MATLAB sortdisplay function');
            s.AxesHandle = bar(0:length(data)-1, data);
            xlim([-1 length(data)]);
        end
        
        function settitle(self, t)
            title( t,'FontSize',18,'FontName','Comic Sans MS');
        end
       
        function update(self, data)
            set(self.AxesHandle, 'YData', data);
            %drawnow;
        end
    end

Subject: Using classes in a compiled library

From: Peter Webb

Date: 17 Mar, 2009 19:22:42

Message: 4 of 5

Specify the functions you want to call on the MCC command line, something
like this:

c:\> mcc -W libMyObj -T link:lib sortdisplay settitle update

This will generate a C interface (in libMyObj.dll) to each of these
functions. MCC cannot automatically create a C++ class from a MATLAB class,
but you can create functional interfaces to MATLAB class methods.


"Andrew Shepherd" <shepherd_a@htotmail.dontspamme.com> wrote in message
news:gpmle1$seb$1@fred.mathworks.com...
> "Steven Lord" <slord@mathworks.com> wrote in message
> <gplkqu$t89$1@fred.mathworks.com>...
>>
>> "Andrew Shepherd" <shepherd_a@htotmail.dontspamme.com> wrote in message
>> news:gplbpe$f3l$1@fred.mathworks.com...
>> > Using the compiler toolbox, it is possible to create a DLL from which
>> > you
>> > can call MATLAB functions that you wrote.
>> >
>> > What about class methods? It seems that if you compile a .m file
>> > containing a classdef, you can call the constructor, but that's it. You
>> > can't call any of the methods.
>> >
>> > Am I missing something, or do I have to resign myself to not using the
>> > object oriented features if I want to write components in compiled
>> > libraries?
>>
>> I was able to define a class with a method, create an M-file function
>> that
>> constructs an object of that class and calls the method, compile the
>> M-file
>> and execute it without any problems in the R2009a release.
>>
>> 1) What versions of MATLAB and MATLAB Compiler are you using? You can
>> find
>> this information using the VER function.
>>
>> 2) Do you receive an error message either during the compilation or (if
>> compilation succeeded) during execution?
>>
>> 3) How are you determining that the methods you tried to call are not
>> being
>> called? [Are they supposed to display a message and they don't, are they
>> supposed to write to a file and they don't, do they give the wrong
>> answer,
>> etc.]
>>
>> 4) Is your class defined all in one file or in multiple files? If the
>> latter, are you including them in the compilation process?
>>
>> 5) Do you have a _small_ example that demonstrates the problem?
>>
>> --
>> Steve Lord
>> slord@mathworks.com
>>
>
> I'm ussing MATLAB r2008b.
>
> I want to create a .DLL file that contains the class functionality. Then,
> via C++, I want to create an instance of this MATLAB class, and then
> invoke a method on this class.
>
> I am making the assumption that I am unable to call these methods, because
> these methods simply do not appear in the generated .H file.
>
> The class I've compiled is below.
>
> I can call the sortdisplay constructor, but the functions "settitle" and
> "update" are absent. Using Visual Studio's "dumpbin" utility, these are
> the functions exported from the DLL.
>
>
> ordinal hint RVA name
>
> 1 0 000018E0 ?sortdisplay@@YAXHAAVmwArray@@ABV1@@Z
> 2 1 000014F0 SortDisplayGetMcrID
> 3 2 000014B0 SortDisplayInitialize
> 4 3 00001440 SortDisplayInitializeWithHandlers
> 5 4 00001500 SortDisplayPrintStackTrace
> 6 5 000014D0 SortDisplayTerminate
> 7 6 00001570 mlxSortdisplay
>
>
>
> Here is the class I compiled.
>
> classdef sortdisplay < handle
> properties (SetAccess = private)
> AxesHandle
> FigureHandle
> end
> methods
> function s = sortdisplay(data)
> % s.FigureHandle = figure();
> disp('Inside the MATLAB sortdisplay function');
> s.AxesHandle = bar(0:length(data)-1, data);
> xlim([-1 length(data)]);
> end
>
> function settitle(self, t)
> title( t,'FontSize',18,'FontName','Comic Sans MS');
> end
>
> function update(self, data)
> set(self.AxesHandle, 'YData', data);
> %drawnow;
> end
> end

Subject: Using classes in a compiled library

From: Andrew Shepherd

Date: 17 Mar, 2009 21:57:01

Message: 5 of 5

"Peter Webb" <pwebb@mathworks.com> wrote in message <gpota2$b3m$1@fred.mathworks.com>...
> Specify the functions you want to call on the MCC command line, something
> like this:
>
> c:\> mcc -W libMyObj -T link:lib sortdisplay settitle update
>
> This will generate a C interface (in libMyObj.dll) to each of these
> functions. MCC cannot automatically create a C++ class from a MATLAB class,
> but you can create functional interfaces to MATLAB class methods.
>
This command seaches for files called "settitle.m" and "update.m". In the example I'm describing, the "settitle" and "update" functions are inside "sortdisplay.m".

So I get this error:
Depfun error: 'Unable to locate settitle as a function on the MATLAB path'

Tags for this Thread

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.

Contact us