Thread Subject: Deep copy of cell array with handle-object members

Subject: Deep copy of cell array with handle-object members

From: Daniel

Date: 7 Jul, 2008 17:12:02

Message: 1 of 6

I have a cell array that may at times contain an object of a
user-defined handle-based class. I'd like to make a deep
copy of the entire cell array, including deep copies of the
cell array's user-defined class objects.

I am using the following code to generate a deep copy of a
class that may contain subclasses, but it does not work with
cell arrays:

function new = copy(this)
new = feval(class(this));
p = fieldnames(this);
for i = 1:length(p)
    if isobject(this.(p{i}))
       new.(p{i}) = copy(this.(p{i}));
    elseif isa(new.(p{i}),'cell')
       new.(p{i}) = mxDuplicateArray(this.(p{i}));
    else
       new.(p{i}) = this.(p{i});
    end
end
end

Any help? Thanks.

Subject: Deep copy of cell array with handle-object members

From: Loren Shure

Date: 8 Jul, 2008 13:34:34

Message: 2 of 6

In article <g4tip2$i4b$1@fred.mathworks.com>,
anothermathgeek+matlab@gmail.com says...
> I have a cell array that may at times contain an object of a
> user-defined handle-based class. I'd like to make a deep
> copy of the entire cell array, including deep copies of the
> cell array's user-defined class objects.
>
> I am using the following code to generate a deep copy of a
> class that may contain subclasses, but it does not work with
> cell arrays:
>
> function new = copy(this)
> new = feval(class(this));
> p = fieldnames(this);
> for i = 1:length(p)
> if isobject(this.(p{i}))
> new.(p{i}) = copy(this.(p{i}));
> elseif isa(new.(p{i}),'cell')
> new.(p{i}) = mxDuplicateArray(this.(p{i}));
> else
> new.(p{i}) = this.(p{i});
> end
> end
> end
>
> Any help? Thanks.
>

Why do you need the deep copy? What is happening that MATLAB isn't
doing the right thing automatically for you?


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

Subject: Deep copy of cell array with handle-object members

From: Daniel

Date: 8 Jul, 2008 14:35:03

Message: 3 of 6

I need it to spawn a new object that is exactly the same,
but be able to edit each independently. For example, if my
class stored the information for a person, (let's say I have
a 'person' class), I may want to copy the person and only
edit their name and age if they live in the same household.
This would be easier than creating a new person from
scratch, and re-entering all of the parameters.

As I have it now, if the class contains a cell array, which
itself contains a user-defined object, a deep copy of the
user-defined object is not made, and the two 'people' share
a handle to the same object (if the object is in a cell
array). I can make a deep copy of an object using the code
above, but I have no way of controlling the '=' operator
used to copy cell arrays so that deep copies are made.

Subject: Deep copy of cell array with handle-object members

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 8 Jul, 2008 18:22:01

Message: 4 of 6

In article <g4vtun$qv9$1@fred.mathworks.com>,
Daniel <anothermathgeek+matlab@gmail.com> wrote:
>I need it to spawn a new object that is exactly the same,
>but be able to edit each independently. For example, if my
>class stored the information for a person, (let's say I have
>a 'person' class), I may want to copy the person and only
>edit their name and age if they live in the same household.
>This would be easier than creating a new person from
>scratch, and re-entering all of the parameters.

That doesn't sound like a need for a deep copy; that sounds like
a need for a shallow copy followed by changing the parameters that
need to be changed.

A shallow copy would be suitable for cases where attributes
near the leaf of the object need to be changed. A deep copy would
be suitable for the case where you want to create an entire new
parallel country with parallel states/provinces and parallel cities
and so on.

Quick test for you: if the city were to be officially renamed,
such as from "Bombay" to "Mumbai", then do you only want to have
to touch the one city object that all the individuals are inheriting
from, or do you want to have to search each object individually and verify
that the appropriate attributes (city name, state/province name,
country name) have identifiers that test equal to the old identifiers,
and change the appropriate attribute to the new ones?

--
Current spam load: 750-800 messages per day (March 4, 2008)

Subject: Deep copy of cell array with handle-object members

From: Dave Foti

Date: 8 Jul, 2008 18:29:18

Message: 5 of 6

Hi Daniel,

One way to get what you want might be to recursively call your copy function
on elements of cell and struct arrays. There is no way to get MATLAB to do
this when copying cells because the MATLAB copy for a handle object copies
the handle rather than creating a new object. This is how I think I would
implement this:

function new = copy(this)
  new = feval(class(this));
  p = fieldnames(this);
  for i = 1:length(p)
    if isobject(this.(p{i}))
      new.(p{i}) = copy(this.(p{i}));
    elseif isa(new.(p{i}),'cell')
      new.(p{i}) = copycell(this.(p{i}));
    elseif isa(new.(p{i}),'struct')
      new.(p{i}) = copystruct(this.(p{i}));
    else
      new.(p{i}) = this.(p{i});
    end
  end
end
function CC = copycell(C)
  n = prod(size(C));
  C = cell(size(C));
  for k = 1:n
  CC{k} = copy(C{k}); % copy each cell
  end
end
function SC = copystruct(S)
  C = copycell(struct2cell(S));
  SC = cell2struct(C, fieldnames(S));
end

"Daniel " <anothermathgeek+matlab@gmail.com> wrote in message
news:g4tip2$i4b$1@fred.mathworks.com...
>I have a cell array that may at times contain an object of a
> user-defined handle-based class. I'd like to make a deep
> copy of the entire cell array, including deep copies of the
> cell array's user-defined class objects.


Subject: Deep copy of cell array with handle-object members

From: Bruno Luong

Date: 8 Jul, 2008 19:07:02

Message: 6 of 6

Why do you need a deep copy? From my understanding, MATLAB
is smart enough to take care automatically of duplicating
subfields/sub-elements.

Example:

c={1 {2 3} 4}
cc = c % "Shalow" copy

cc{2}{2} % 3
c{2}{2} % 3

cc{2}{2}=5 % Change a sub field

cc{2}{2} % 5
c{2}{2} % 3

cc{2}{1} % 2

Can you give a case where "deep copy" is needed?

Bruno


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
handle class Daniel 7 Jul, 2008 13:15:12
cellarray Daniel 7 Jul, 2008 13:15:12
cell array Daniel 7 Jul, 2008 13:15:11
deepcopy Daniel 7 Jul, 2008 13:15:10
deep copy Daniel 7 Jul, 2008 13:15:10
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com