0

Update: Check accepted solution. Problem was irrelevant to class properties.

I have following problem in Matlab and cannot find anything on google or here. I have a handle class like this:

classdef myClass < handle
  properties
    hugeCellArray
    otherVariables
  end
  ...
end

I instanciate it as myObj = myClass(data); Now if I try: clear myObj.hugeCellArray it doesn't clear the property at all. If I try myObj.hugecellArray = []; it does set the property as [] but the memory is still allocated! So it seems like I cannot get rid of a huge variable in a class unless I clear the whole class? Thanks in advance!

Edit: (Clarification) My problem is the memory. I don't want to actually delete the class property, I want to free its memory.

4

3 回答 3

4

You cant "delete" a property of an object without clearing the entire object itself.

If your concern is about memory, then what you tried does indeed clear the allocated memory:

myObj = myClass();
myObj.prop = rand(5000);
memory
myObj.prop = [];
memory
于 2013-06-26T13:05:39.663 回答
1

Solution by Amro:

Apparently the problem seems to be in cell arrays. They fragment my memory and it cannot be reused afterwards. Only workaround seems to be concatenating the cell array into one huge array and keeping tabs of where each subarray originally started.

于 2013-06-27T08:33:01.893 回答
0

Maybe you want to find the answer like:

d.a = 3;
d.b = 4;
c = 3;
clear d.a

Both d.b and c still exist.

于 2020-12-21T01:54:55.060 回答