Avoid data duplication when using SuperClass

This is my first time developing a MATLAB OOP project. My parent class will have a very large matrix, which children (many) should have access to. How can I prevent children from copying data?

In pseudo code, I require that

classdef parent properties largeMatrix; end end classdef child < parent methods function obj = child(parent) Data.parent of this child = Share from parent end ... something = largeMatrix(n,m); end end p = parent; p.largeMatrix = rand(100); c1 = child(p); c2 = child(p); 

Both users c1 and c2 should access the same data that was created in the original rand(100) , but should not copy largeMatrix , since I need a lot of children, and I would like the program to be memory efficient. largeMatrix will be read from the file.

PS: This is the first time I am posting this forum, so forgive me if I posted it incorrectly.

+4
source share
2 answers

A class is just a data type; it does not contain any data. If you create an instance of the parent object of class parent , and then the child object of class child , then by default child does not inherit any data from parent . You can copy data from one object to another using the copy constructor.

 child = parent; % this copies the data in `parent` to `child` 

However, in this case, Matlab creates a copy of the data in parent .

You can avoid copying data using descriptor objects . You can assign the handle object to multiple variables or pass it to functions without forcing MATLAB to make a copy of the original object. For instance,

 classdef A < handle properties largeMatrix; % wrap your large data into a handle class end end classdef B properties data; end methods function obj = B(mydata) obj.data = mydata; end end end 

Then in the main program you can assign

 a = A(); a.largeMatrix = rand(100); b1 = B(a); b2 = B(a); b3 = b1; % can even do this % no copies of largeMatrix were made, because `a` is a handle object % accessing largeMatrix b1.data.largeMatrix b3.data.largeMatrix 
+1
source

MATLAB copies the array when writing .

Suppose your class is parent (no need to subclass the handle):

 classdef parent properties largeMatrix; end end 

and your child class:

 classdef child < parent methods function obj = child(parent) obj.largeMatrix = parent.largeMatrix; end end end 

Now create a parent element and assign a large matrix to your largeMatrix property:

 p = parent; p.largeMatrix = rand(1e4); % 750 MB circa 

Check out the jump in memory:

enter image description here

Now create a child and make sure the data has been pointed to:

 c = child(p); size(c.largeMatrix) 

As you do not see a jump in memory:

enter image description here

Finally, make a simple change to the child data:

 c.largeMatrix(1) = 1; 

As you can see, only when writing the array is effectively copied :

enter image description here

To prevent writing to child.largeMatrix , define a property in the parent class with the attribute (Abstract = true) and in the child (SetAccess = immutable) .

+2
source

All Articles