Thread Subject: calling a function when a class is loaded for the first time

Subject: calling a function when a class is loaded for the first time

From: Timothee

Date: 14 Nov, 2009 18:31:01

Message: 1 of 25

Is there a way to call a function when a class is loaded for the first time?
something like:

classdef A
methods(Static)
end
end

function on_load
% do useful things like adding a path required for functions inside this to work
% (and this shouldn't be called until the first reference to class A, either static or nonstatic)
end

Subject: calling a function when a class is loaded for the first time

From: Matt

Date: 14 Nov, 2009 21:55:03

Message: 2 of 25

"Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message <hdmt15$lo2$1@fred.mathworks.com>...
> Is there a way to call a function when a class is loaded for the first time?
> something like:
>
> classdef A
> methods(Static)
> end
> end
>
> function on_load
> % do useful things like adding a path required for functions inside this to work
> % (and this shouldn't be called until the first reference to class A, either static or nonstatic)
> end


You could write a loadobj() method for the class and use a persistent variable to monitor whether it's been called for the first time or not.

Subject: calling a function when a class is loaded for the first time

From: Timothee

Date: 15 Nov, 2009 02:11:03

Message: 3 of 25

"Matt " <xys@whatever.com> wrote in message <hdn8vn$1q0$1@fred.mathworks.com>...
> "Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message <hdmt15$lo2$1@fred.mathworks.com>...
> > Is there a way to call a function when a class is loaded for the first time?
> > something like:
> >
> > classdef A
> > methods(Static)
> > end
> > end
> >
> > function on_load
> > % do useful things like adding a path required for functions inside this to work
> > % (and this shouldn't be called until the first reference to class A, either static or nonstatic)
> > end
>
>
> You could write a loadobj() method for the class and use a persistent variable to monitor whether it's been called for the first time or not.

Thanks; however I think loadobj is called when we load a mat file containing an object of class A. What I want is a method that is invoked when the class is loaded in memory for the first time via a static method invocation (it would be easy to do for a non-static method invocation, by putting the persistent flag in the constructor).
Any ideas would be appreciated....

Subject: calling a function when a class is loaded for the first time

From: Loren Shure

Date: 16 Nov, 2009 14:37:14

Message: 4 of 25

In article <hdmt15$lo2$1@fred.mathworks.com>,
timothee.cour_remove_these_@gmail.com says...
> Is there a way to call a function when a class is loaded for the first time?
> something like:
>
> classdef A
> methods(Static)
> end
> end
>
> function on_load
> % do useful things like adding a path required for functions inside this to work
> % (and this shouldn't be called until the first reference to class A, either static or nonstatic)
> end
>

Keep some state information in the class and check it when you create an
object. Skip the code after the first time it's run.

--
Loren
http://blogs.mathworks.com/loren

Subject: calling a function when a class is loaded for the first time

From: Timothee

Date: 16 Nov, 2009 14:50:19

Message: 5 of 25

Loren Shure <loren.shure@mathworks.com> wrote in message <MPG.256b2da21e3d248d989a74@news.mathworks.com>...
> In article <hdmt15$lo2$1@fred.mathworks.com>,
> timothee.cour_remove_these_@gmail.com says...
> > Is there a way to call a function when a class is loaded for the first time?
> > something like:
> >
> > classdef A
> > methods(Static)
> > end
> > end
> >
> > function on_load
> > % do useful things like adding a path required for functions inside this to work
> > % (and this shouldn't be called until the first reference to class A, either static or nonstatic)
> > end
> >
>
> Keep some state information in the class and check it when you create an
> object. Skip the code after the first time it's run.
>
> --
> Loren
> http://blogs.mathworks.com/loren

bu that won't work if a static method is invoked....

Subject: calling a function when a class is loaded for the first time

From: Rich Ellis

Date: 16 Nov, 2009 19:23:06

Message: 6 of 25

How about something like this:

classdef OnLoad
   properties
      LoadFcn = OnLoad.on_load;
   end
   methods (Static)
      function o = on_load
         try
            addpath('H:/Mat','-begin');
         catch me
         end
         if exist('me','var')
            o = me;
         else
            o = 'OK';
         end
      end
   end
end

The expression assigned to a property is evaluated when the class is first
loaded.


"Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message
news:hdrorb$c3q$1@fred.mathworks.com...
> Loren Shure <loren.shure@mathworks.com> wrote in message
> <MPG.256b2da21e3d248d989a74@news.mathworks.com>...
>> In article <hdmt15$lo2$1@fred.mathworks.com>,
>> timothee.cour_remove_these_@gmail.com says...
>> > Is there a way to call a function when a class is loaded for the first
>> > time?
>> > something like:
>> >
>> > classdef A
>> > methods(Static)
>> > end
>> > end
>> >
>> > function on_load
>> > % do useful things like adding a path required for functions inside
>> > this to work
>> > % (and this shouldn't be called until the first reference to class A,
>> > either static or nonstatic)
>> > end
>> >
>>
>> Keep some state information in the class and check it when you create an
>> object. Skip the code after the first time it's run.
>>
>> --
>> Loren
>> http://blogs.mathworks.com/loren
>
> bu that won't work if a static method is invoked....
>

Subject: calling a function when a class is loaded for the first time

From: Timothee

Date: 16 Nov, 2009 20:05:19

Message: 7 of 25

"Rich Ellis" <rich@mathworks.com> wrote in message <hds8qs$t4v$1@fred.mathworks.com>...
> How about something like this:
>
> classdef OnLoad
> properties
> LoadFcn = OnLoad.on_load;
> end
> methods (Static)
> function o = on_load
> try
> addpath('H:/Mat','-begin');
> catch me
> end
> if exist('me','var')
> o = me;
> else
> o = 'OK';
> end
> end
> end
> end
>
> The expression assigned to a property is evaluated when the class is first
> loaded.
>
>
> "Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message
> news:hdrorb$c3q$1@fred.mathworks.com...
> > Loren Shure <loren.shure@mathworks.com> wrote in message
> > <MPG.256b2da21e3d248d989a74@news.mathworks.com>...
> >> In article <hdmt15$lo2$1@fred.mathworks.com>,
> >> timothee.cour_remove_these_@gmail.com says...
> >> > Is there a way to call a function when a class is loaded for the first
> >> > time?
> >> > something like:
> >> >
> >> > classdef A
> >> > methods(Static)
> >> > end
> >> > end
> >> >
> >> > function on_load
> >> > % do useful things like adding a path required for functions inside
> >> > this to work
> >> > % (and this shouldn't be called until the first reference to class A,
> >> > either static or nonstatic)
> >> > end
> >> >
> >>
> >> Keep some state information in the class and check it when you create an
> >> object. Skip the code after the first time it's run.
> >>
> >> --
> >> Loren
> >> http://blogs.mathworks.com/loren
> >
> > bu that won't work if a static method is invoked....
> >
>


Again, this doesn't work for a static method invocation.
I feel that this is a missing useful feature that is present in several OOP languages. It would be easy to support, for example with a keyword methods(onload)

proof:

calling "A.test" will not call the initialize() function.

classdef A
    properties
        is_loaded=initialize();
    end
    methods(Static)
        function test
            disp('I am called but initialize has not been called yet');
        end
    end
end

function is_loaded=initialize(); % I also tried putting it as a method to no avail...
disp('class loading for first time')
is_loaded=1;
end

Subject: calling a function when a class is loaded for the first time

From: Matt

Date: 16 Nov, 2009 20:45:20

Message: 8 of 25

"Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message <hdsb9v$6qv$1@fred.mathworks.com>...

> Again, this doesn't work for a static method invocation.
> I feel that this is a missing useful feature that is present in several OOP languages. It would be easy to support, for example with a keyword methods(onload)
>
> proof:
>
> calling "A.test" will not call the initialize() function.
>
> classdef A
> properties
> is_loaded=initialize();
> end
> methods(Static)
> function test
> disp('I am called but initialize has not been called yet');
> end
> end
> end
>
> function is_loaded=initialize(); % I also tried putting it as a method to no avail...
> disp('class loading for first time')
> is_loaded=1;
> end

Now I don't understand. Why is it necessary for your purposes that A.test call the initialize() function or that a Static method be involved here at all? Isn't it enough that initialize() is called when the class is first loaded? Can't you put your desired initialization steps entirely in initialize() and let it be called by the property initialization?

Subject: calling a function when a class is loaded for the first time

From: Matt

Date: 16 Nov, 2009 21:38:02

Message: 9 of 25

"Matt " <xys@whatever.com> wrote in message <hdsdl0$38a$1@fred.mathworks.com>...

> Now I don't understand. Why is it necessary for your purposes that A.test call the initialize() function or that a Static method be involved here at all?
========================

Forget it. I think I understand.

Well, why not call the initialize() function at the beginning of all of your static methods (and the constructor), but use a persistent variable inside initialize() to keep track of whether it needs to do any actual work:


classdef myClass

    methods
        function obj=myClass
         initialize;
        end
    end
    methods(Static)
        function StaticMethod1
           initialize;
        end
        
        function StaticMethod2
           initialize;
        end
        
        
    end
end

function out=initialize(); % I also tried putting it as a method to no avail...

 persistent is_loaded;

 if isempty(is_loaded)
  disp('Initializing...')
  is_loaded=1;
 else
  disp 'Already initialized'
 end
 
 out=is_loaded;
end

====================

Now, all of your various cases are covered:

>> clear classes; A=myClass; myClass.StaticMethod1; myClass.StaticMethod2;
Initializing...
Already initialized
Already initialized
>> clear classes; myClass.StaticMethod1; A=myClass; myClass.StaticMethod2;
Initializing...
Already initialized
Already initialized
>> clear classes; myClass.StaticMethod1;myClass.StaticMethod2;A=myClass;
Initializing...
Already initialized
Already initialized

Subject: calling a function when a class is loaded for the first time

From: Timothee

Date: 16 Nov, 2009 21:41:01

Message: 10 of 25

"Matt " <xys@whatever.com> wrote in message <hdsdl0$38a$1@fred.mathworks.com>...
> "Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message <hdsb9v$6qv$1@fred.mathworks.com>...
>
> > Again, this doesn't work for a static method invocation.
> > I feel that this is a missing useful feature that is present in several OOP languages. It would be easy to support, for example with a keyword methods(onload)
> >
> > proof:
> >
> > calling "A.test" will not call the initialize() function.
> >
> > classdef A
> > properties
> > is_loaded=initialize();
> > end
> > methods(Static)
> > function test
> > disp('I am called but initialize has not been called yet');
> > end
> > end
> > end
> >
> > function is_loaded=initialize(); % I also tried putting it as a method to no avail...
> > disp('class loading for first time')
> > is_loaded=1;
> > end
>
> Now I don't understand. Why is it necessary for your purposes that A.test call the initialize() function or that a Static method be involved here at all? Isn't it enough that initialize() is called when the class is first loaded? Can't you put your desired initialization steps entirely in initialize() and let it be called by the property initialization?


***************************
What I want is ability to do static initialization (for example as in C++ http://www.codeproject.com/KB/cpp/static_init.aspx)

Again, if the function initialize() does something that is useful to all static member functions in the class (such as adding a path required for those, etc), then this will be called at the first reference (static/nonstatic) to the class.

>>> Isn't it enough that initialize() is called when the class is first loaded?
what do you mean by first loaded ? do you mean loaded during the first object construction? The class can be called without having an object constructed via static members. The example I showed demonstrates that it fails to call initialize()

Subject: calling a function when a class is loaded for the first time

From: Matt

Date: 17 Nov, 2009 16:22:19

Message: 11 of 25

"Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message <hdsgtd$qe5$1@fred.mathworks.com>...

> Again, if the function initialize() does something that is useful to all static member functions in the class (such as adding a path required for those, etc), then this will be called at the first reference (static/nonstatic) to the class.
=============

Well, the solution I proposed in my last post will accomplish this. I don't know if you were trying to avoid the work of prepending all your static methods with a call to initialize()...

Subject: calling a function when a class is loaded for the first time

From: Timothee

Date: 17 Nov, 2009 16:37:22

Message: 12 of 25

"Matt " <xys@whatever.com> wrote in message <hduijr$qch$1@fred.mathworks.com>...
> "Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message <hdsgtd$qe5$1@fred.mathworks.com>...
>
> > Again, if the function initialize() does something that is useful to all static member functions in the class (such as adding a path required for those, etc), then this will be called at the first reference (static/nonstatic) to the class.
> =============
>
> Well, the solution I proposed in my last post will accomplish this. I don't know if you were trying to avoid the work of prepending all your static methods with a call to initialize()...


yes, I was trying to avoid that...
I guess it's not possible then without prepending in every static method. Thanks for your time...

Subject: calling a function when a class is loaded for the first time

From: Matt

Date: 25 Nov, 2009 10:41:04

Message: 13 of 25

"Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message <hdujg2$nud$1@fred.mathworks.com>...
> "Matt " <xys@whatever.com> wrote in message <hduijr$qch$1@fred.mathworks.com>...
> > "Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message <hdsgtd$qe5$1@fred.mathworks.com>...
> >
> > > Again, if the function initialize() does something that is useful to all static member functions in the class (such as adding a path required for those, etc), then this will be called at the first reference (static/nonstatic) to the class.
> > =============
> >
> > Well, the solution I proposed in my last post will accomplish this. I don't know if you were trying to avoid the work of prepending all your static methods with a call to initialize()...
>
>
> yes, I was trying to avoid that...
> I guess it's not possible then without prepending in every static method. Thanks for your time...
=========================

It looks like I spoke to soon! !!!

It turns out that we can avoid prepending initialize() to every static method by calling it instead from the Attribute block of the classdef line, as in the example below. Basically, the idea is that you can call a function to set a classdef Attribute and this call is made only the first time the class is accessed (whether statically or non-statically). So, you just pick any class attribute, use initialize() to set the Attribute to its default value (if the default is indeed the value you wish), and do anything else inside the function that you want (set-up paths etc...)

Admittedly, this is a somewhat cheesy workaround, but it does get the job done with only a single call to initialize(). The only hitch I found is that initialize() must be defined outside the classdef file, presumably because classdef Attribute definitions cannot reference the class being defined.

%%%%%%%%%%%%%%%%%%%%%%

EXAMPLE:


classdef (Sealed=initialize) myClass

    methods
        function obj=myClass
          disp 'Constructor called'
        end
    end
    methods(Static)
        function StaticMethod1
           disp 'Static Method called'
        end

    end
end


function out=initialize(); %THIS MUST BE DEFINED OUTSIDE THE CLASSDEF FILE!!!


 disp ' '
 disp 'Initializing at classdef time'
 disp ' '
 
 out=false; %default value of the Sealed attribute
 
end


>> clear classes; myClass.StaticMethod1; A=myClass;
 
Initializing at classdef time
 
Static Method called
Constructor called

>> clear classes; A=myClass; myClass.StaticMethod1;
 
Initializing at classdef time
 
Constructor called
Static Method called

Subject: calling a function when a class is loaded for the first time

From: Timothee

Date: 25 Nov, 2009 12:29:05

Message: 14 of 25

"Matt " <xys@whatever.com> wrote in message <hej1k0$o5c$1@fred.mathworks.com>...
> "Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message <hdujg2$nud$1@fred.mathworks.com>...
> > "Matt " <xys@whatever.com> wrote in message <hduijr$qch$1@fred.mathworks.com>...
> > > "Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message <hdsgtd$qe5$1@fred.mathworks.com>...
> > >
> > > > Again, if the function initialize() does something that is useful to all static member functions in the class (such as adding a path required for those, etc), then this will be called at the first reference (static/nonstatic) to the class.
> > > =============
> > >
> > > Well, the solution I proposed in my last post will accomplish this. I don't know if you were trying to avoid the work of prepending all your static methods with a call to initialize()...
> >
> >
> > yes, I was trying to avoid that...
> > I guess it's not possible then without prepending in every static method. Thanks for your time...
> =========================
>
> It looks like I spoke to soon! !!!
>
> It turns out that we can avoid prepending initialize() to every static method by calling it instead from the Attribute block of the classdef line, as in the example below. Basically, the idea is that you can call a function to set a classdef Attribute and this call is made only the first time the class is accessed (whether statically or non-statically). So, you just pick any class attribute, use initialize() to set the Attribute to its default value (if the default is indeed the value you wish), and do anything else inside the function that you want (set-up paths etc...)
>
> Admittedly, this is a somewhat cheesy workaround, but it does get the job done with only a single call to initialize(). The only hitch I found is that initialize() must be defined outside the classdef file, presumably because classdef Attribute definitions cannot reference the class being defined.
>
> %%%%%%%%%%%%%%%%%%%%%%
>
> EXAMPLE:
>
>
> classdef (Sealed=initialize) myClass
>
> methods
> function obj=myClass
> disp 'Constructor called'
> end
> end
> methods(Static)
> function StaticMethod1
> disp 'Static Method called'
> end
>
> end
> end
>
>
> function out=initialize(); %THIS MUST BE DEFINED OUTSIDE THE CLASSDEF FILE!!!
>
>
> disp ' '
> disp 'Initializing at classdef time'
> disp ' '
>
> out=false; %default value of the Sealed attribute
>
> end
>
>
> >> clear classes; myClass.StaticMethod1; A=myClass;
>
> Initializing at classdef time
>
> Static Method called
> Constructor called
>
> >> clear classes; A=myClass; myClass.StaticMethod1;
>
> Initializing at classdef time
>
> Constructor called
> Static Method called


Hi,
thanks, that is clever. It would however be more useful if one were allowed to defined initialize inside the same file. Would you consider adding this relatively simple feature in the next matlab release? (say, with another keyword to avoid resorting to the hack). For example:

classdef (Initialize=initialize) myClass
end

function initialize();
end

Subject: calling a function when a class is loaded for the first time

From: Matt

Date: 25 Nov, 2009 12:40:21

Message: 15 of 25

"Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message <hej7uh$j1u$1@fred.mathworks.com>...

> Hi,
> thanks, that is clever. It would however be more useful if one were allowed to defined initialize inside the same file. Would you consider adding this relatively simple feature in the next matlab release? (say, with another keyword to avoid resorting to the hack).
==========================

No, probably not.

Subject: calling a function when a class is loaded for the first time

From: Matt

Date: 25 Nov, 2009 13:06:04

Message: 16 of 25

"Matt " <xys@whatever.com> wrote in message <hej8jl$1m$1@fred.mathworks.com>...
> "Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message <hej7uh$j1u$1@fred.mathworks.com>...
>
> > Hi,
> > thanks, that is clever. It would however be more useful if one were allowed to defined initialize inside the same file. Would you consider adding this relatively simple feature in the next matlab release? (say, with another keyword to avoid resorting to the hack).
> ==========================
>
> No, probably not.

I'm just messing with you. :-)

It seems like a reasonable enough feature to add, but since I don't work for The Mathworks, what I would "consider adding" is pretty much moot.

You can always submit an enhancement request form, but of course there's no way to know what priority it will receive...

Subject: calling a function when a class is loaded for the first time

From: Steven Lord

Date: 26 Nov, 2009 22:14:10

Message: 17 of 25


"Matt " <xys@whatever.com> wrote in message
news:hej1k0$o5c$1@fred.mathworks.com...
> "Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message
> <hdujg2$nud$1@fred.mathworks.com>...
>> "Matt " <xys@whatever.com> wrote in message
>> <hduijr$qch$1@fred.mathworks.com>...
>> > "Timothee " <timothee.cour_remove_these_@gmail.com> wrote in message
>> > <hdsgtd$qe5$1@fred.mathworks.com>...

*snip*

> Admittedly, this is a somewhat cheesy workaround, but it does get the job
> done with only a single call to initialize(). The only hitch I found is
> that initialize() must be defined outside the classdef file, presumably
> because classdef Attribute definitions cannot reference the class being
> defined.

You can define the initialization function inside the classdef file but
outside the classdef _block_ if you want. You can't define it as a _method
of the object_, though, as you don't have an instance of the object to pass
into the method at the time it's called.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: calling a function when a class is loaded for the first time

From: Matt

Date: 26 Nov, 2009 22:23:03

Message: 18 of 25

"Steven Lord" <slord@mathworks.com> wrote in message <hemuh5$ou0$1@fred.mathworks.com>...

> You can define the initialization function inside the classdef file but
> outside the classdef _block_ if you want.

Strangely, that doesn't work. It produces an error, which I've reported here:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/266809#697416

Subject: calling a function when a class is loaded for the first time

From: Steven Lord

Date: 28 Nov, 2009 04:16:27

Message: 19 of 25


"Matt " <xys@whatever.com> wrote in message
news:hemv47$1gk$1@fred.mathworks.com...
> "Steven Lord" <slord@mathworks.com> wrote in message
> <hemuh5$ou0$1@fred.mathworks.com>...
>
>> You can define the initialization function inside the classdef file but
>> outside the classdef _block_ if you want.
>
> Strangely, that doesn't work. It produces an error, which I've reported
> here:
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/266809#697416

Which version of MATLAB are you using? I just tried it in MATLAB 7.9
(release R2009b) and it worked.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: calling a function when a class is loaded for the first time

From: Matt J

Date: 28 Nov, 2009 15:12:03

Message: 20 of 25

"Steven Lord" <slord@mathworks.com> wrote in message <heq84d$re6$1@fred.mathworks.com>...
>

> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/266809#697416
>
> Which version of MATLAB are you using? I just tried it in MATLAB 7.9
> (release R2009b) and it worked.

I'm using R2009b also. Did you check with your colleagues (Philip Borghesani) who responded in the other thread I posted this to. Apparently, they were able to reproduce the error.

http://www.mathworks.com/matlabcentral/newsreader/view_thread/266809#697416

Subject: calling a function when a class is loaded for the first time

From: Matt J

Date: 28 Nov, 2009 15:33:06

Message: 21 of 25

"Steven Lord" <slord@mathworks.com> wrote in message <heq84d$re6$1@fred.mathworks.com>...
>
> "Matt " <xys@whatever.com> wrote in message
> news:hemv47$1gk$1@fred.mathworks.com...
> > "Steven Lord" <slord@mathworks.com> wrote in message
> > <hemuh5$ou0$1@fred.mathworks.com>...
> >
> >> You can define the initialization function inside the classdef file but
> >> outside the classdef _block_ if you want.
> >
> > Strangely, that doesn't work. It produces an error, which I've reported
> > here:
> >
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/266809#697416
>
> Which version of MATLAB are you using? I just tried it in MATLAB 7.9
> (release R2009b) and it worked.

Are we definitely talking about the same thing? I have no problem calling a subfunction defined inside the classdef M-file from a property or class method. The error appears when you try to call it from the class Attributes block.

Subject: calling a function when a class is loaded for the first time

From: Steven Lord

Date: 28 Nov, 2009 23:34:05

Message: 22 of 25


"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
news:herek3$690$1@fred.mathworks.com...
> "Steven Lord" <slord@mathworks.com> wrote in message
> <heq84d$re6$1@fred.mathworks.com>...
>>
>
>> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/266809#697416
>>
>> Which version of MATLAB are you using? I just tried it in MATLAB 7.9
>> (release R2009b) and it worked.
>
> I'm using R2009b also. Did you check with your colleagues (Philip
> Borghesani) who responded in the other thread I posted this to.
> Apparently, they were able to reproduce the error.
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/266809#697416

I have not; I have been out of the office for Thanksgiving week. But if
Philip has reproduced the behavior, I'll let him handle it. It's possible
he and I are interpreting what you've described differently, and he's found
a bug using his reproduction steps.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: calling a function when a class is loaded for the first time

From: Wesley

Date: 29 Nov, 2009 10:13:04

Message: 23 of 25

The other posters were close to what I think is the best answer. The trick is that the dummy property initializer should be for a static property:

    classdef StaticInitObject < handle
        properties (Constant,Access=private) % Note Constant attribute
            onInitDummy = StaticInitObject.StaticInit();
        end
        methods (Static)
            function SayHello()
                fprintf( 'StaticInitObject.SayHello\n' );
            end
        end
        methods (Static,Access=private)
            function ret = StaticInit()
                fprintf( 'StaticInitObject.StaticInit\n' );
                ret = [];
            end
        end
    end

Test by running the public static method:

    >> StaticInitObject.SayHello();
    StaticInitObject.StaticInit
    StaticInitObject.SayHello

Subject: calling a function when a class is loaded for the first time

From: Matt J

Date: 29 Nov, 2009 12:55:06

Message: 24 of 25

"Wesley " <not.an.email@example.com> wrote in message <hethff$cpq$1@fred.mathworks.com>...
> The other posters were close to what I think is the best answer. The trick is that the dummy property initializer should be for a static property:
>
> classdef StaticInitObject < handle
> properties (Constant,Access=private) % Note Constant attribute
> onInitDummy = StaticInitObject.StaticInit();
> end
> methods (Static)
> function SayHello()
> fprintf( 'StaticInitObject.SayHello\n' );
> end
> end
> methods (Static,Access=private)
> function ret = StaticInit()
> fprintf( 'StaticInitObject.StaticInit\n' );
> ret = [];
> end
> end
> end
>
> Test by running the public static method:
>
> >> StaticInitObject.SayHello();
> StaticInitObject.StaticInit
> StaticInitObject.SayHello


This is intriguing, but also very puzzling. Clearly, a call to the static method
SayHello() somehow causes the Constant property onInitDummy to be initialized, but why would this be the case? The static method isn't supposed to need or be able to access an instance or property of the class, so why would a call to the static method trigger this initialization?

Subject: calling a function when a class is loaded for the first time

From: Wesley

Date: 30 Nov, 2009 11:21:18

Message: 25 of 25

Just like SayHello is a static method of the class, onInitDummy is a static property of the class (as opposed to an instance property). (Note that because all static properties are read-only in MATLAB, they use the Constant attribute instead of the Static attribute.)
So SayHello could indeed access the onInitDummy property, with the expression:

    StaticInitObject.onInitDummy

It's possible you're getting confused because MATLAB is a bit strange in letting you access static members (methods and properties) through an instance. For example, the following are legal:

    obj = StaticInitObject();
    obj.StaticInit();
    x = obj.onInitDummy;

Or at least they would be legal, if I hadn't marked those members as private.

So, it is the correct behaviour for MATLAB to initialize all static properties when the class is first loaded. Hope that helps.

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
obscure behavior Matt J 29 Nov, 2009 07:59:54
oop per isakson 16 Nov, 2009 15:11:08
load Timothee 14 Nov, 2009 13:34:03
class Timothee 14 Nov, 2009 13:34:03
persistent Timothee 14 Nov, 2009 13:34:03
static Timothee 14 Nov, 2009 13:34:03
rssFeed for this Thread

Contact us at files@mathworks.com