Thread Subject: making an independent copy of a matlab object

Subject: making an independent copy of a matlab object

From: Daniel

Date: 16 Jun, 2008 15:50:20

Message: 1 of 9

I have written a class using the 'handle' parent class. I
understand that the default behaivior for derived classes
of this type is that when a copy is made (using the copy
constructor):

>> a = myclass;
>> b = myclass(a);

b is just a pointer to the object a, and essentially, they
share the same local variables. For example:

>> a.localvariable = 'a';
>> b.localvariable = 'b';
>> a.localVariable
ans =
b

I need to be able to make a complete, independent copy of
an object. How is this done most easily?

Thanks!
Daniel

Subject: making an independent copy of a matlab object

From: James Tursa

Date: 16 Jun, 2008 17:08:04

Message: 2 of 9

"Daniel " <anothermathgeek+matlab@gmail.com> wrote in
message <g3623s$hmb$1@fred.mathworks.com>...
> I have written a class using the 'handle' parent class.
I
> understand that the default behaivior for derived
classes
> of this type is that when a copy is made (using the copy
> constructor):
>
> >> a = myclass;
> >> b = myclass(a);
>
> b is just a pointer to the object a, and essentially,
they
> share the same local variables. For example:
>
> >> a.localvariable = 'a';
> >> b.localvariable = 'b';
> >> a.localVariable
> ans =
> b
>
> I need to be able to make a complete, independent copy
of
> an object. How is this done most easily?
>
> Thanks!
> Daniel

I don't know if this is the easiest way, but you could
write a simple c-mex file to do this:

#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
     const mxArray *prhs[])
{
    plhs[0] = mxDuplicateArray(prhs[0]);
}

mxDuplicateArray will make a "deep" copy of all levels of
the input variable. The above code can be compiled (one-
time only) as follows assuming you have named the file
deepcopy.c and placed it somewhere on the MATLAB path:

mex -setup
(then press Enter, then pick Lcc)
mex deepcopy.c

Now you have a function called deepcopy that returns a
copy of the input variable. You should probably add in
error checking code as well (check number of inputs &
outputs).

James Tursa

Subject: making an independent copy of a matlab object

From: Doug Schwarz

Date: 16 Jun, 2008 20:11:14

Message: 3 of 9

In article <g3623s$hmb$1@fred.mathworks.com>,
 "Daniel " <anothermathgeek+matlab@gmail.com> wrote:

> I have written a class using the 'handle' parent class. I
> understand that the default behaivior for derived classes
> of this type is that when a copy is made (using the copy
> constructor):
>
> >> a = myclass;
> >> b = myclass(a);
>
> b is just a pointer to the object a, and essentially, they
> share the same local variables. For example:
>
> >> a.localvariable = 'a';
> >> b.localvariable = 'b';
> >> a.localVariable
> ans =
> b
>
> I need to be able to make a complete, independent copy of
> an object. How is this done most easily?
>
> Thanks!
> Daniel


When I had to do this I added this method to my class:

        % Make a copy of a handle object.
        function new = copy(this)
            % Instantiate new object of the same class.
            new = feval(class(this));
 
            % Copy all non-hidden properties.
            p = properties(this);
            for i = 1:length(p)
                new.(p{i}) = this.(p{i});
            end
        end


Then, when I want to make a copy of

  a = myclass;

I use something like this:

  b = copy(a); % which reads nicely as b = copy of a

or, equivalently,

  b = a.copy;


I don't see an easy way to copy hidden properties other than by listing
them in the copy method, e.g.,

  new.a_hidden = this.a_hidden;
  new.b_hidden = this.b_hidden;

etc. or perhaps just building the p = properties(this) cell array
manually.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: making an independent copy of a matlab object

From: Daniel

Date: 19 Jun, 2008 14:29:01

Message: 4 of 9

Thanks a ton for the posts. That's exactly what I'm looking
for. However, the call to "p = properties(this)" returns the
following error:
_______________________________________________________
??? Undefined function or method 'properties' for input
arguments of type 'stage'.

Error in ==> stage.stage>stage.copy at 120
            p = properties(this);
_______________________________________________________

Is properties an automatic property of the class? Matlab
doesn't seem to know what it is.

Thanks,
Daniel

Subject: making an independent copy of a matlab object

From: Douglas Schwarz

Date: 19 Jun, 2008 14:47:02

Message: 5 of 9

"Daniel " <anothermathgeek+matlab@gmail.com> wrote in
message <g3dqfd$6sa$1@fred.mathworks.com>...
> Thanks a ton for the posts. That's exactly what I'm looking
> for. However, the call to "p = properties(this)" returns the
> following error:
> _______________________________________________________
> ??? Undefined function or method 'properties' for input
> arguments of type 'stage'.
>
> Error in ==> stage.stage>stage.copy at 120
> p = properties(this);
> _______________________________________________________
>
> Is properties an automatic property of the class? Matlab
> doesn't seem to know what it is.
>
> Thanks,
> Daniel

Hi Daniel,

I assumed you were using R2008a -- perhaps properties is a
new feature in that version.

Try using fieldnames instead. It should work the same way.

  p = fieldnames(this);

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: making an independent copy of a matlab object

From: Daniel

Date: 19 Jun, 2008 15:52:01

Message: 6 of 9

Yes, I'm still in the dark ages here (R2007a). 'fieldnames'
did the trick. Thanks a ton!

Subject: making an independent copy of a matlab object

From: M.

Date: 15 Jul, 2008 19:25:03

Message: 7 of 9

>
> I don't see an easy way to copy hidden properties other
than by listing
> them in the copy method, e.g.,
>
> new.a_hidden = this.a_hidden;
> new.b_hidden = this.b_hidden;
>
> etc. or perhaps just building the p = properties(this)
cell array
> manually.
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

I created another method to find the hidden properties in a
class. It hasn't been rigorously tested, but it might be
useful to some, so here it is:


function hiddenProps = findHidden(this)
fid = fopen( which( class(this) ) );
if fid < 0
    hiddenProps = {};
    return
end
hiddenProps = {};
foundHidden = 0;

while ~feof(fid)
    ln = fgetl( fid );
    if ~isempty( regexpi( ln,
'\s*properties\s*\(\s*Hidden\s*\)' ) )
        foundHidden = 1;
        continue
    end
    if foundHidden == 1;
        if ~isempty( regexpi( ln, '\s*end' ) )
            break
        end
        if isempty( regexpi( ln, '\w*' ) )
            continue
        end
        hiddenProps{end+1} = strtrim( strtok( ln, '=' ) );
        hiddenProps{end} = strtrim( strtok(
hiddenProps{end}, ';' ) );
    else
        continue
    end
end
fclose( fid );
end % END FINDHIDDEN method

Subject: making an independent copy of a matlab object

From: M.

Date: 15 Jul, 2008 19:25:04

Message: 8 of 9

>
> I don't see an easy way to copy hidden properties other
than by listing
> them in the copy method, e.g.,
>
> new.a_hidden = this.a_hidden;
> new.b_hidden = this.b_hidden;
>
> etc. or perhaps just building the p = properties(this)
cell array
> manually.
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

I created another method to find the hidden properties in a
class. It hasn't been rigorously tested, but it might be
useful to some, so here it is:


function hiddenProps = findHidden(this)
fid = fopen( which( class(this) ) );
if fid < 0
    hiddenProps = {};
    return
end
hiddenProps = {};
foundHidden = 0;

while ~feof(fid)
    ln = fgetl( fid );
    if ~isempty( regexpi( ln,
'\s*properties\s*\(\s*Hidden\s*\)' ) )
        foundHidden = 1;
        continue
    end
    if foundHidden == 1;
        if ~isempty( regexpi( ln, '\s*end' ) )
            break
        end
        if isempty( regexpi( ln, '\w*' ) )
            continue
        end
        hiddenProps{end+1} = strtrim( strtok( ln, '=' ) );
        hiddenProps{end} = strtrim( strtok(
hiddenProps{end}, ';' ) );
    else
        continue
    end
end
fclose( fid );
end % END FINDHIDDEN method

Subject: making an independent copy of a matlab object

From: Doug Schwarz

Date: 16 Jul, 2008 00:26:20

Message: 9 of 9

In article <g5itig$nfl$1@fred.mathworks.com>,
 "M. " <mdp.public.we.dont.need.no.stinking.spam@gmail.com> wrote:

> >
> > I don't see an easy way to copy hidden properties other
> than by listing
> > them in the copy method, e.g.,
> >
> > new.a_hidden = this.a_hidden;
> > new.b_hidden = this.b_hidden;
> >
> > etc. or perhaps just building the p = properties(this)
> cell array
> > manually.
> >
> > --
> > Doug Schwarz
> > dmschwarz&ieee,org
> > Make obvious changes to get real email address.
>
> I created another method to find the hidden properties in a
> class. It hasn't been rigorously tested, but it might be
> useful to some, so here it is:


[snip]

I have since discovered that fieldnames(struct(obj)) will return all
property names.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

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
deepcopy James Tursa 16 Jun, 2008 13:10:25
copy Daniel 16 Jun, 2008 11:55:05
class Daniel 16 Jun, 2008 11:55:05
object Daniel 16 Jun, 2008 11:55:05
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