Performance when creating an array of class objects

2 views (last 30 days)
Hi
Using Matlab 2010b Service Pack 1. We are having a performance issue when creating an array of 1000 objects.
The array is obj.MyClassArray
The objects are of class
classdef MyClass < handle
properties
prop1
prop2
prop3
nameValueContainerMap % a container.Map(name, MySeriesObject)
end
The map class is
classdef MySeries < handle
properties
dates
values % a cell containing n doubles
end
The cell consists of an array of doubles.
We are pre-allocating memory by initialising obj.MyClassArray with 1000 copies of a fully populated object of MyClass, using repmat.
However, we are still finding a performance issue.
Any ideas?
Thanks
Marty

Answers (1)

Adam
Adam on 29 Aug 2014
If your class has a default constructor (or basically your constructor can handle the 0-argument case) then the simplest way to pre-allocate an array of 1000 objects is just to type:
myArray(1,1000) = MyClass;
You say "1000 copies of a fully populated object" though which suggests it isn't infact pre-allocation you are doing, but full array allocation already initialised objects. In that case you can't do what I suggested.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!