Search Comments and Ratings

go

   
Date File Comment by Comment Rating
25 Jun 2013 Design Pattern: Singleton (Creational) A MATLAB® OOP implementation of the Singleton Creational Design Pattern Author: Bobby Nedelkovski Abel

Abel (view profile)

Not thread safe as far as I can tell. Nor any documentation even addressing the issue.

3
16 Nov 2012 Design Pattern: Singleton (Creational) A MATLAB® OOP implementation of the Singleton Creational Design Pattern Author: Bobby Nedelkovski Ron Lu

Ron Lu (view profile)

Nice example of singleton class. Thank you!

5
18 Nov 2011 Design Pattern: Singleton (Creational) A MATLAB® OOP implementation of the Singleton Creational Design Pattern Author: Bobby Nedelkovski Brad Stiritz

Thank you Bobby! for your excellent implementation of this classic design pattern. I especially appreciate your extensive documentation & excellent, highly-descriptive coding style.
5-stars well-deserved. If only all MATLAB code could look like this! Highly recommended.

5
17 Nov 2011 Clone Handle Object - using MATLAB OOP How to clone an object (deep copy) which inherits reference behaviour from the 'handle' class. Author: Bobby Nedelkovski Shaw

Shaw (view profile)

for a handle class contains another handle object. I would do deep copy "recursively". (the following one might face the loop situation.)

classdef B < handle
properties
a % A obj
end
methods
function this = B()
this.a = A();
end

% class A should have the SIMILAR
% clonePublic function
function newObj = clonePublic(this)
props = properties(this);
newObj = B();
for i = 1: length(props)
tmp = this.(props{i});
if(isa(tmp,'handle'))
newObj.(props{i}) = tmp.clonePublic();
else
newObj.(props{i}) = tmp ;
end
end
end
end
end

Comment only
17 Nov 2011 Clone Handle Object - using MATLAB OOP How to clone an object (deep copy) which inherits reference behaviour from the 'handle' class. Author: Bobby Nedelkovski Shaw

Shaw (view profile)

test

Comment only
07 Sep 2011 Clone Handle Object - using MATLAB OOP How to clone an object (deep copy) which inherits reference behaviour from the 'handle' class. Author: Bobby Nedelkovski Jason

Jason (view profile)

As of R2011A Matlab provides some sort of means for cloning/copying handle objects. Inherit from the matlab.mixin.Copyable class.

It does seem to have a lot of restrictions, but has been good enough for my needs at the moment.

Comment only
06 Oct 2010 Clone Handle Object - using MATLAB OOP How to clone an object (deep copy) which inherits reference behaviour from the 'handle' class. Author: Bobby Nedelkovski Markus Leuthold

classdef A < handle
properties
b = containers.Map
end
end

please note that containers.Map is itself a handle class, which is not covered in the copyobj function from Volkmar Glauche. You could recursively call copyobj for handle properties, but then you're facing the next problem of private properties (Maps has private props), which cannot be copied as already mentioned.

In order to copy this class, the "ugly" solution from Barry Ridge is unfortunately the only thing which works in order to deep copy an instance of class A.

any news on this issue from Mathworks?

best regards
kusi

Comment only
07 Jun 2010 Clone Handle Object - using MATLAB OOP How to clone an object (deep copy) which inherits reference behaviour from the 'handle' class. Author: Bobby Nedelkovski Bobby Nedelkovski

Thank you for your suggestions Barry and Holger. I recall encountering Holger's File Exchange posting some time ago and I was going to suggest this in response to Barry's feedback - this is a good workaround given the trade-offs.

Comment only
06 Jun 2010 Clone Handle Object - using MATLAB OOP How to clone an object (deep copy) which inherits reference behaviour from the 'handle' class. Author: Bobby Nedelkovski Holger Hiebel

I came up with the same "hack" as Barry - this now was already a few years ago... From a programming point of view this is very unaesthetic but it does what it is supposed to do.
I posted it on file exchange as contribution 20972 "copymyobj".
Using this hack is more powerful than any other implementation of a generic copy because it deep-copies also dynamically added properties etc. This is due to reasons which are described by Bobby some 2 postings above.

More than once I complained at The Mathworks that there is no generic function for doing deep copies of classes inheriting from handles - obviously now there is a chance that they are investigating this issue :-)

Comment only
03 Jun 2010 Clone Handle Object - using MATLAB OOP How to clone an object (deep copy) which inherits reference behaviour from the 'handle' class. Author: Bobby Nedelkovski Barry Ridge

I came up with a bit of a hack for doing this. It uses file-handling, and therefore is not so elegant, but it seems to work. Just add this method to whatever class you want to copy:

function new = copy(this)
save('temp.mat', 'this');
Foo = load('temp.mat');
new = Foo.this;
delete('temp.mat');
end

Example usage (assumes copy() is a method in class MyClass):
A = MyClass();
B = A.copy();

Comment only
11 Apr 2010 Data Structure: A Cell Array List Container Provides a useful 1D container for storing an ordered heterogeneous set of elements Author: Bobby Nedelkovski Bobby Nedelkovski

Thank you for your feedback and suggestions Joao.

I appreciate you pointing out the bug with the add() method in class CellArrayList. The bug (infinite while-loop) occurs when capacity = numElts and remove() is called numElts consecutive times to reduce capacity to 0. Subsequently, if you call add(), the while-loop used to re-allocate memory for the list cell array container won’t exit!

I have since fixed this issue and have submitted an update which will be published on the File Exchange soon.

Your suggestions of overloading critical MATLAB system methods such as ‘subsref’ and ‘subsasgn’ for the short-term benefits of element/property replacement is dangerous. Experienced MATLAB programmers will know that natural index notation is used to index into arrays of data types e.g. array of structs or array of cell arrays or array of char arrays or in this case, array of CellArrayLists!

Incidentally, I have just uncovered another bug with reference to how I’ve implemented all my methods with respect to the creation of arrays of CellArrayLists – i.e. myList = CellArrayList(); myList(2) = CellArrayList(); – and the way in which I’ve overloaded system methods ‘isempty’, ‘length’ and ‘display’!

This just goes to show that you need to be extremely cautious when deciding to overload MATLAB system methods! However, I like the idea of having a specialised ‘replace’ method which will effectively execute the ‘remove’ method followed by the ‘add’ method. In this way, you can easily replace elements in the list yet according to OOP practice, it is still perfectly acceptable for programmers to retrieve ADT elements and simply modify properties (this is all you need to do for objects of reference behaviour) and insert them back into the list (for objects of copy behaviour).

Joao, please feel free to contribute to the development of the ‘replace’ method. I can review your code if you send it to me by email which you can find off my File Exchange profile page (just click on the hyperlink ‘Bobby Nedelkovski’ on this page).

Comment only
05 Apr 2010 Data Structure: A Cell Array List Container Provides a useful 1D container for storing an ordered heterogeneous set of elements Author: Bobby Nedelkovski Joao Henriques

Bug found: if you remove all elements, capacity will be 0. It will then, when adding a new element, enter an infinite loop (!) trying to multiply the capacity by 2 until it reaches the desired number of elements, which will never happen. Since the simple behavior of "adding some elements, removing them, adding some more" was never tested, I'm starting to get the impression that this class never got much use by the author... =|

4
01 Apr 2010 Data Structure: A Cell Array List Container Provides a useful 1D container for storing an ordered heterogeneous set of elements Author: Bobby Nedelkovski Joao Henriques

Ah, one important feature is missing: "set". You can "get" elements, but not write to them! Removing and then adding incurs unnecessary overhead. It would also be nice if you overloaded standard methods such as "subsref" and "subsasgn" so we can use index notation instead of get/set methods.

Comment only
31 Mar 2010 Data Structure: A Cell Array List Container Provides a useful 1D container for storing an ordered heterogeneous set of elements Author: Bobby Nedelkovski Joao Henriques

Pretty cool! Now if there was a realization of the abstract class as a C-implemented linked list...

5
20 Sep 2009 Design Pattern: Iterator (Behavioural) A MATLAB® OOP implementation of the Iterator Behavioural Design Pattern Author: Bobby Nedelkovski Bobby Nedelkovski

Thank you for your feedback Kun-Chul.

To provide clarification to the comment "good implementation for the oo design principle: polymorphism!!":

The CellArrayList implementation of the List ADT provides 'polymorphism' at the element level as I've indicated in File Exchange item #25024 by stating that CellArrayList is a storage container for a *heterogeneous* set of elements.

On the other hand, polymorphism at the list object creation level can't be generally enforced since MATLAB is loosly typed. Ideally, to conform with good software engineering practice, it would be beneficial to declare an identifier using an interface/abstract class so you just need to change the instantiating class if you simply want to switch from one implementation of List to another (i.e. you cannot perform "List myList = CellArrayList();" so then you would simply need to modify this line to use another implementation "List myList = AnotherImplList();"). So in this case, polymorphism is not apparent in MATLAB OOP.

On the alternative implementation of method 'locationsOf' (i.e. "locs = find(cellfun('isclass',obj.list,class(elt)));"), I have asserted in the comments for 'locationsOf' in abstract class 'List.m' that it should return the locations of all occurances of a single element in the list; speed shouldn't be necessarily the issue here. Hence why I use a strict equality test in "locs = find(cellfun(@(c)isequal(c,elt),obj.list));" *not* simply checking the element type. For example, if I define a list of the sort "x = [1,2,{3,4;5,6},{7,8;9,10},11]" and test x.locationsOf(2), the 'isequal' implementations yields the correct result of 2. The 'isclass' implementation will yield the relative locations of elements 1,2 and 11 given their data type matches the data type of the input argument. This is then contrary to the "contractual agreement" defined in the comments for 'locationsOf' in the abstract class 'List.m'. Sure, though you are welcome to have specialised methods in your custom subclasses of the List ADT - for example, perhaps an 'isclass' operation could be be encapsulated in a method called 'classTypeLocationsOf'.

Finally, to comment on "I hope Mathworks to develop cell array vectorization for matlab oop...", the idea of this CellArrayList realisation of the List ADT and OOP in general is to hide the low-level detail in implementation of certain functionality (like performing "find(cellarray..." or even for that matter "find(listObj==obj)" as suggested) since the user of CellArrayList only needs to know how to use 'locationsOf'. So the onus is on the developer to create their own implementations of specific class methods using base MATLAB functionality; therefore OOP shouldn't be seen as a replacement for or thought of as superseding base MATLAB functionality since functions like 'cellfun' still have their place.

Comment only
20 Sep 2009 Data Structure: A Cell Array List Container Provides a useful 1D container for storing an ordered heterogeneous set of elements Author: Bobby Nedelkovski Bobby Nedelkovski

Thank you for your feedback Kun-Chul.

To provide clarification to the comment "good implementation for the oo design principle: polymorphism!!":

The CellArrayList implementation of the List ADT provides 'polymorphism' at the element level as I've indicated in File Exchange item #25024 by stating that CellArrayList is a storage container for a *heterogeneous* set of elements.

On the other hand, polymorphism at the list object creation level can't be generally enforced since MATLAB is loosly typed. Ideally, to conform with good software engineering practice, it would be beneficial to declare an identifier using an interface/abstract class so you just need to change the instantiating class if you simply want to switch from one implementation of List to another (i.e. you cannot perform "List myList = CellArrayList();" so then you would simply need to modify this line to use another implementation "List myList = AnotherImplList();"). So in this case, polymorphism is not apparent in MATLAB OOP.

On the alternative implementation of method 'locationsOf' (i.e. "locs = find(cellfun('isclass',obj.list,class(elt)));"), I have asserted in the comments for 'locationsOf' in abstract class 'List.m' that it should return the locations of all occurances of a single element in the list; speed shouldn't be necessarily the issue here. Hence why I use a strict equality test in "locs = find(cellfun(@(c)isequal(c,elt),obj.list));" *not* simply checking the element type. For example, if I define a list of the sort "x = [1,2,{3,4;5,6},{7,8;9,10},11]" and test x.locationsOf(2), the 'isequal' implementations yields the correct result of 2. The 'isclass' implementation will yield the relative locations of elements 1,2 and 11 given their data type matches the data type of the input argument. This is then contrary to the "contractual agreement" defined in the comments for 'locationsOf' in the abstract class 'List.m'. Sure, though you are welcome to have specialised methods in your custom subclasses of the List ADT - for example, perhaps an 'isclass' operation could be be encapsulated in a method called 'classTypeLocationsOf'.

Finally, to comment on "I hope Mathworks to develop cell array vectorization for matlab oop...", the idea of this CellArrayList realisation of the List ADT and OOP in general is to hide the low-level detail in implementation of certain functionality (like performing "find(cellarray..." or even for that matter "find(listObj==obj)" as suggested) since the user of CellArrayList only needs to know how to use 'locationsOf'. So the onus is on the developer to create their own implementations of specific class methods using base MATLAB functionality; therefore OOP shouldn't be seen as a replacement for or thought of as superseding base MATLAB functionality since functions like 'cellfun' still have their place.

Comment only
18 Sep 2009 Design Pattern: Iterator (Behavioural) A MATLAB® OOP implementation of the Iterator Behavioural Design Pattern Author: Bobby Nedelkovski Kun-Chul

good implementation for the oo design principle: polymorphism!!

I also used cell array for the objects container ( list container)
for the objects from different classes (but, those different classes inhereted from a same super class)

cell list class is more useful and powerful for the object list container.

however, try this code for the 'locationOf' method (much faster):
locs = find(cellfun('isclass',obj.list,class(elt))
, since cellfun is slow when suing function handle
(but, know the limitation of the code above:
if obj.list contains the object from a same class, then 'find' function returns matching index array.

I hope Mathworks to develop cell array vectorization for matlab oop

for example, objList is cell array contain

find(objList==obj) %this function does not support for cell array

while find(objArray==obj) is supported when objArray is array container of the object from a same class.

5
18 Sep 2009 Data Structure: A Cell Array List Container Provides a useful 1D container for storing an ordered heterogeneous set of elements Author: Bobby Nedelkovski Kun-Chul

good implementation for the oo design principle: polymorphism!!

I also used cell array for the objects container ( list container)
for the objects from different classes (but, those different classes inhereted from a same super class)

cell list class is more useful and powerful for the object list container.

however, try this code for the 'locationOf' method (much faster):
locs = find(cellfun('isclass',obj.list,class(elt))
, since cellfun is slow when suing function handle
(but, know the limitation of the code above:
if obj.list contains the object from a same class, then 'find' function returns matching index array.

I hope Mathworks to develop cell array vectorization for matlab oop

for example, objList is cell array contain

find(objList==obj) %this function does not support for cell array

while find(objArray==obj) is supported when objArray is array container of the object from a same class.

Comment only
04 Aug 2009 Clone Handle Object - using MATLAB OOP How to clone an object (deep copy) which inherits reference behaviour from the 'handle' class. Author: Bobby Nedelkovski Bobby Nedelkovski

This File Exchange item is now under review. WARNING: Neither the original method (leveraging ‘TestClass.m’ with the ‘copyobj’ method as a template for your custom class) nor using Volkmar’s suggestion (inherit the Volkmar’s proposed ‘copyobj’ method by subclassing ‘TestClass.m’) produces a truly ‘deep’ copy of a handle object. Instead, both methods yield ‘deep’ copies of public properties in the custom superclass hierarchy yet they produce ‘shallow’ copies of private and protected properties. This has to do with the depth in scope of private and protected properties to an instance…

Case: Values of private properties declared in any superclass or protected properties declared in any grandparent class or higher cannot be copied. Accessor methods for private and protected properties can be customised – for instance, getMyProperty(obj). If a private property is modified with a custom set method and since this custom method is not part of the property’s meta-data (i.e. “GetMethod = []” in its corresponding “meta.property” object), one cannot copy the value of the private property using simple assignment with Dynamic Expressions. Moreover, you cannot temporarily modify the property’s “GetMethod=getMyProperty(obj)” nor “GetAccess=Public” meta-data fields to assume necessary control to copy private properties.

I am currently investigating possibilities in producing a truly ‘deep’ copy of a handle object. Please feel free to email any suggestions to bobby.nedelkovski@mathworks.com.au.

Comment only
26 Jun 2009 Clone Handle Object - using MATLAB OOP How to clone an object (deep copy) which inherits reference behaviour from the 'handle' class. Author: Bobby Nedelkovski Volkmar Glauche

3
25 Jun 2009 Clone Handle Object - using MATLAB OOP How to clone an object (deep copy) which inherits reference behaviour from the 'handle' class. Author: Bobby Nedelkovski Volkmar Glauche

This is the code I used for copyobj:
function new = copyobj(obj)
% This method acts as a copy constructor for all derived
% classes.
new = feval(class(obj)); % create new object of correct subclass.
mobj = metaclass(obj);
% Only copy properties which are
% * not dependent or dependent and have a SetMethod
% * not constant
% * not abstract
% * defined in this class or have public SetAccess - not
% sure whether this restriction is necessary
sel = find(cellfun(@(cProp)(~cProp.Constant && ...
~cProp.Abstract && ...
(~cProp.Dependent || ...
(cProp.Dependent && ...
~isempty(cProp.SetMethod))),mobj.Properties));
for k = sel(:)'
new.(mobj.Properties{k}.Name) = obj.(mobj.Properties{k}.Name);
end
end

Comment only
25 Jun 2009 Clone Handle Object - using MATLAB OOP How to clone an object (deep copy) which inherits reference behaviour from the 'handle' class. Author: Bobby Nedelkovski Volkmar Glauche

I also faced this problem, but for more complicated objects (using e.g. protected and dependent properties). It turned out that just looping over all properties did not do the trick. I had to check all kind of meta-properties to make sure the copyobj method didn't crash.

Comment only

Contact us