Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
structures as input to a function

Subject: structures as input to a function

From: Dimitar Dimitrov

Date: 20 Jun, 2008 18:55:03

Message: 1 of 10

Hello,

What happens when Matlab passes a data
structure to a function?

Does Matlab make a copy of each elements in this structure?

Suppose I have a structure ST that contains n entries:

ST.e1
ST.e2
....
ST.en

If I want to change only element ST.e2 (which depends only
on ST.e1 and ST.e3) in a function 'my_func', I can make a
call to my_func as:

ST.e2 = my_func(ST.e1, ST.e3) ...(option 1)

or I can make directly

ST.e2 = my_func(ST) ...(option 2)

or

ST = my_func(ST) ...(option 3)

I prefer the last case because I can assign some
value to ST.e2 at the end of my_func, and the user
does not need to care about whats going in and whats going out.

I am wondering what is the "penalty" I am paying when using
(option 3)

Thank you
Dimitar

Subject: structures as input to a function

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

Date: 20 Jun, 2008 19:05:51

Message: 2 of 10

In article <g3gue7$p6r$1@fred.mathworks.com>,
Dimitar Dimitrov <mail_mitko@example.com> wrote:

>What happens when Matlab passes a data
>structure to a function?

>Does Matlab make a copy of each elements in this structure?

No.

Each field of each array member of the structure array is treated
as an independant entity with "copy on write" semantics.

However, if you modify a structure within a routine and Matlab
is not able to figure out that you are outputting into the same
structure (and there is no other sharing of the data), then within
the routine if you write to part of the structure, Matlab must
construct a complete new structure whose data pointers are all the
same except for the changed field. This is faster than copying
the entire structure with all of its data, but it is faster to
not modify the structure within the routine.
--
  "Eightly percent of the people in the world are fools and the
  rest of us are in danger of contamination." -- Walter Matthau

Subject: structures as input to a function

From: Dimitar Dimitrov

Date: 20 Jun, 2008 20:09:02

Message: 3 of 10

Thank you Walter,

This means that there will be no difference between
(option 1) and (option 2) provided that the structure
ST is not modified within my_func.

I am wondering whether it would be possible for me to
directly modify a field of a structure using pointers
SOMEHOW :)?

Regards,
Dimitar

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g3gv2f$emi$1@canopus.cc.umanitoba.ca>...
> In article <g3gue7$p6r$1@fred.mathworks.com>,
> Dimitar Dimitrov <mail_mitko@example.com> wrote:
>
> >What happens when Matlab passes a data
> >structure to a function?
>
> >Does Matlab make a copy of each elements in this structure?
>
> No.
>
> Each field of each array member of the structure array is
treated
> as an independant entity with "copy on write" semantics.
>
> However, if you modify a structure within a routine and Matlab
> is not able to figure out that you are outputting into the
same
> structure (and there is no other sharing of the data),
then within
> the routine if you write to part of the structure, Matlab must
> construct a complete new structure whose data pointers are
all the
> same except for the changed field. This is faster than copying
> the entire structure with all of its data, but it is faster to
> not modify the structure within the routine.
> --
> "Eightly percent of the people in the world are fools
and the
> rest of us are in danger of contamination." -- Walter
Matthau

Subject: structures as input to a function

From: Doug Schwarz

Date: 20 Jun, 2008 21:51:31

Message: 4 of 10

[top posting repaired]

In article <g3h2ot$j0l$1@fred.mathworks.com>,
 "Dimitar Dimitrov" <mail_mitko@example.com> wrote:

> roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
> message <g3gv2f$emi$1@canopus.cc.umanitoba.ca>...
> > In article <g3gue7$p6r$1@fred.mathworks.com>,
> > Dimitar Dimitrov <mail_mitko@example.com> wrote:
> >
> > >What happens when Matlab passes a data
> > >structure to a function?
> >
> > >Does Matlab make a copy of each elements in this structure?
> >
> > No.
> >
> > Each field of each array member of the structure array is
> treated
> > as an independant entity with "copy on write" semantics.
> >
> > However, if you modify a structure within a routine and Matlab
> > is not able to figure out that you are outputting into the
> same
> > structure (and there is no other sharing of the data),
> then within
> > the routine if you write to part of the structure, Matlab must
> > construct a complete new structure whose data pointers are
> all the
> > same except for the changed field. This is faster than copying
> > the entire structure with all of its data, but it is faster to
> > not modify the structure within the routine.
> > --
> > "Eightly percent of the people in the world are fools
> and the
> > rest of us are in danger of contamination." -- Walter
> Matthau
>
> Thank you Walter,
>
> This means that there will be no difference between
> (option 1) and (option 2) provided that the structure
> ST is not modified within my_func.

Correct.


> I am wondering whether it would be possible for me to
> directly modify a field of a structure using pointers
> SOMEHOW :)?


I think you want to implement your structure as a handle object
(warning: untested code!):

--------- myclass.m -------------
classdef myclass < handle
  properties
    e1
    e2
    e3
  end
  methods
    function my_func(this)
      this.e2 = this.e1 + this.e3; % or some other function
    end
  end
end
---------------------------------

To use it:

  >> a = myclass;
  >> a.e1 = 5;
  >> a.e3 = 7;
  >> a.my_func
  >> a.e2
  ans =
       12

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

Subject: structures as input to a function

From: Dimitar Dimitrov

Date: 20 Jun, 2008 22:50:03

Message: 5 of 10

Hello Doug,

WOW I did not know that Matlab has such functionality.
This will improve my code structure a lot.

Just to make sure:
I am using Matlab ver. R2007b
is the same concept supported for earlier Matlab versions?

I am asking because I have been using Matlab like forever
and I did not know about this!

Thank you!

Regards,
Dimitar


I define 'myclass' with some 'properties' and
'methods', and


Doug Schwarz <see@sig.for.address.edu> wrote in message
<see-A6FE21.17513120062008@news.motzarella.org>...
> [top posting repaired]
>
> In article <g3h2ot$j0l$1@fred.mathworks.com>,
> "Dimitar Dimitrov" <mail_mitko@example.com> wrote:
>
> > roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
> > message <g3gv2f$emi$1@canopus.cc.umanitoba.ca>...
> > > In article <g3gue7$p6r$1@fred.mathworks.com>,
> > > Dimitar Dimitrov <mail_mitko@example.com> wrote:
> > >
> > > >What happens when Matlab passes a data
> > > >structure to a function?
> > >
> > > >Does Matlab make a copy of each elements in this
structure?
> > >
> > > No.
> > >
> > > Each field of each array member of the structure array is
> > treated
> > > as an independant entity with "copy on write" semantics.
> > >
> > > However, if you modify a structure within a routine
and Matlab
> > > is not able to figure out that you are outputting into the
> > same
> > > structure (and there is no other sharing of the data),
> > then within
> > > the routine if you write to part of the structure,
Matlab must
> > > construct a complete new structure whose data pointers are
> > all the
> > > same except for the changed field. This is faster than
copying
> > > the entire structure with all of its data, but it is
faster to
> > > not modify the structure within the routine.
> > > --
> > > "Eightly percent of the people in the world are fools
> > and the
> > > rest of us are in danger of contamination." -- Walter
> > Matthau
> >
> > Thank you Walter,
> >
> > This means that there will be no difference between
> > (option 1) and (option 2) provided that the structure
> > ST is not modified within my_func.
>
> Correct.
>
>
> > I am wondering whether it would be possible for me to
> > directly modify a field of a structure using pointers
> > SOMEHOW :)?
>
>
> I think you want to implement your structure as a handle
object
> (warning: untested code!):
>
> --------- myclass.m -------------
> classdef myclass < handle
> properties
> e1
> e2
> e3
> end
> methods
> function my_func(this)
> this.e2 = this.e1 + this.e3; % or some other function
> end
> end
> end
> ---------------------------------
>
> To use it:
>
> >> a = myclass;
> >> a.e1 = 5;
> >> a.e3 = 7;
> >> a.my_func
> >> a.e2
> ans =
> 12
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Subject: structures as input to a function

From: us

Date: 21 Jun, 2008 00:38:02

Message: 6 of 10

"Dimitar Dimitrov":
<SNIP the beauty and the beast...

> WOW I did not know that Matlab has such functionality...
the beauty...

> I am using Matlab ver. R2007b
the beast...

us, the ugly...

Subject: structures as input to a function

From: Doug Schwarz

Date: 22 Jun, 2008 00:54:55

Message: 7 of 10

[top posting repaired, again]

In article <g3hc6r$m43$1@fred.mathworks.com>,
 "Dimitar Dimitrov" <mail_mitko@example.com> wrote:

> Doug Schwarz <see@sig.for.address.edu> wrote in message
> <see-A6FE21.17513120062008@news.motzarella.org>...
> > [top posting repaired]
> >
> > In article <g3h2ot$j0l$1@fred.mathworks.com>,
> > "Dimitar Dimitrov" <mail_mitko@example.com> wrote:

[snip]

> > > I am wondering whether it would be possible for me to
> > > directly modify a field of a structure using pointers
> > > SOMEHOW :)?
> >
> >
> > I think you want to implement your structure as a handle
> object
> > (warning: untested code!):
> >
> > --------- myclass.m -------------
> > classdef myclass < handle
> > properties
> > e1
> > e2
> > e3
> > end
> > methods
> > function my_func(this)
> > this.e2 = this.e1 + this.e3; % or some other function
> > end
> > end
> > end
> > ---------------------------------
> >
> > To use it:
> >
> > >> a = myclass;
> > >> a.e1 = 5;
> > >> a.e3 = 7;
> > >> a.my_func
> > >> a.e2
> > ans =
> > 12
>
> Hello Doug,
>
> WOW I did not know that Matlab has such functionality.
> This will improve my code structure a lot.
>
> Just to make sure:
> I am using Matlab ver. R2007b
> is the same concept supported for earlier Matlab versions?
>
> I am asking because I have been using Matlab like forever
> and I did not know about this!
>
> Thank you!
>
> Regards,
> Dimitar


Dimitar,

R2008a is the first version in which this new object system is
officially supported, but parts of it did exist in earlier versions.
For example, I think R2007a required the myclass.m file to reside in a
'@myclass' directory, but I don't know if the handle superclass existed
and I don't know how much of this is in R2007b. Give it a try. If it
doesn't work then you have some incentive to upgrade. I especially like
the ability to define a whole class in one file. I always hated having
a separate file for each class method in the old style OOP.

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

Subject: structures as input to a function

From: Dimitar Dimitrov

Date: 22 Jun, 2008 06:39:03

Message: 8 of 10

Doug,

Even though the new way of defining classes is great,
I have a strong feeling that it will undergo major changes
in future releases, so it is still unreasonable to use it
for my project. But it is definitely something to look for.
Thank you for the suggestion.

From my point of view the problem of the old way of defining
classes is not the file/directory structure, but the fact
that it does not provide any advantage over structures (for
the topic discussed in this thread), or does it?
 
It doesn't matter whether I write

ST = myfunction(ST, arg1, ...) % ST is a structure
or
P = myclassmethod(P, arg1, ...) % P is of some class ...

If ST and P are changed within the routine all fields will
be re-constructed (even though most of them are unchanged).
Please, correct me if I am wrong.

Thank you,
Dimitar


Doug Schwarz <see@sig.for.address.edu> wrote in message
<see-C7D487.20545521062008@news.motzarella.org>...
> [top posting repaired, again]
>
> In article <g3hc6r$m43$1@fred.mathworks.com>,
> "Dimitar Dimitrov" <mail_mitko@example.com> wrote:
>
> > Doug Schwarz <see@sig.for.address.edu> wrote in message
> > <see-A6FE21.17513120062008@news.motzarella.org>...
> > > [top posting repaired]
> > >
> > > In article <g3h2ot$j0l$1@fred.mathworks.com>,
> > > "Dimitar Dimitrov" <mail_mitko@example.com> wrote:
>
> [snip]
>
> > > > I am wondering whether it would be possible for me to
> > > > directly modify a field of a structure using pointers
> > > > SOMEHOW :)?
> > >
> > >
> > > I think you want to implement your structure as a handle
> > object
> > > (warning: untested code!):
> > >
> > > --------- myclass.m -------------
> > > classdef myclass < handle
> > > properties
> > > e1
> > > e2
> > > e3
> > > end
> > > methods
> > > function my_func(this)
> > > this.e2 = this.e1 + this.e3; % or some other
function
> > > end
> > > end
> > > end
> > > ---------------------------------
> > >
> > > To use it:
> > >
> > > >> a = myclass;
> > > >> a.e1 = 5;
> > > >> a.e3 = 7;
> > > >> a.my_func
> > > >> a.e2
> > > ans =
> > > 12
> >
> > Hello Doug,
> >
> > WOW I did not know that Matlab has such functionality.
> > This will improve my code structure a lot.
> >
> > Just to make sure:
> > I am using Matlab ver. R2007b
> > is the same concept supported for earlier Matlab versions?
> >
> > I am asking because I have been using Matlab like forever
> > and I did not know about this!
> >
> > Thank you!
> >
> > Regards,
> > Dimitar
>
>
> Dimitar,
>
> R2008a is the first version in which this new object
system is
> officially supported, but parts of it did exist in earlier
versions.
> For example, I think R2007a required the myclass.m file to
reside in a
> '@myclass' directory, but I don't know if the handle
superclass existed
> and I don't know how much of this is in R2007b. Give it a
try. If it
> doesn't work then you have some incentive to upgrade. I
especially like
> the ability to define a whole class in one file. I always
hated having
> a separate file for each class method in the old style OOP.
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Subject: structures as input to a function

From: Doug Schwarz

Date: 23 Jun, 2008 04:15:50

Message: 9 of 10

In article <g3ks27$7cq$1@fred.mathworks.com>,
 "Dimitar Dimitrov" <mail_mitko@example.com> wrote:

[snip]

> Doug,
>
> Even though the new way of defining classes is great,
> I have a strong feeling that it will undergo major changes
> in future releases, so it is still unreasonable to use it
> for my project. But it is definitely something to look for.
> Thank you for the suggestion.

I don't share your concern, but you have to be comfortable with what
you're doing.


> From my point of view the problem of the old way of defining
> classes is not the file/directory structure, but the fact
> that it does not provide any advantage over structures (for
> the topic discussed in this thread), or does it?
>
> It doesn't matter whether I write
>
> ST = myfunction(ST, arg1, ...) % ST is a structure
> or
> P = myclassmethod(P, arg1, ...) % P is of some class ...
>
> If ST and P are changed within the routine all fields will
> be re-constructed (even though most of them are unchanged).
> Please, correct me if I am wrong.

I agree with your analysis. OOP doesn't help you, but let's be clear on
something. If you write

  ST = myfunction(ST,arg1)

and myfunction looks like this:

  function s = myfunction(s,arg)
  s.a = arg;

then, at worst, MATLAB will have to build a new structure to be returned
by the function, but it will *not* have to copy any of the data in any
of the fields. The pointers to the existing data will just be assigned
to the fields of the new structure. This is just as Walter explained.
If you are concerned with how long that might take then I suggest you do
it and time it. Only you know how long is too long.

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

Subject: structures as input to a function

From: Dimitar Dimitrov

Date: 23 Jun, 2008 16:20:18

Message: 10 of 10

Thank you all,

Cheers,
Dimitar

Doug Schwarz <see@sig.for.address.edu> wrote in message
<see-61B188.00155023062008@news.motzarella.org>...
> In article <g3ks27$7cq$1@fred.mathworks.com>,
> "Dimitar Dimitrov" <mail_mitko@example.com> wrote:
>
> [snip]
>
> > Doug,
> >
> > Even though the new way of defining classes is great,
> > I have a strong feeling that it will undergo major changes
> > in future releases, so it is still unreasonable to use it
> > for my project. But it is definitely something to look for.
> > Thank you for the suggestion.
>
> I don't share your concern, but you have to be comfortable
with what
> you're doing.
>
>
> > From my point of view the problem of the old way of defining
> > classes is not the file/directory structure, but the fact
> > that it does not provide any advantage over structures (for
> > the topic discussed in this thread), or does it?
> >
> > It doesn't matter whether I write
> >
> > ST = myfunction(ST, arg1, ...) % ST is a structure
> > or
> > P = myclassmethod(P, arg1, ...) % P is of some class ...
> >
> > If ST and P are changed within the routine all fields will
> > be re-constructed (even though most of them are unchanged).
> > Please, correct me if I am wrong.
>
> I agree with your analysis. OOP doesn't help you, but
let's be clear on
> something. If you write
>
> ST = myfunction(ST,arg1)
>
> and myfunction looks like this:
>
> function s = myfunction(s,arg)
> s.a = arg;
>
> then, at worst, MATLAB will have to build a new structure
to be returned
> by the function, but it will *not* have to copy any of the
data in any
> of the fields. The pointers to the existing data will
just be assigned
> to the fields of the new structure. This is just as
Walter explained.
> If you are concerned with how long that might take then I
suggest you do
> it and time it. Only you know how long is too long.
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Tags for this Thread

No tags are associated with this thread.

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.

Contact us