Got Questions? Get Answers.
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:
Operator overloading in Matlab

Subject: Operator overloading in Matlab

From: Berthold Barzantny

Date: 4 Nov, 2005 10:48:34

Message: 1 of 6

Hello everybody,

Matlab has a limited capability of object-oriented programming. This
includes operator overloading, one of the most important features of
object-oriented programming.

Does anybody know, why it is not possible to overload the assigning
operation of two objects like a=b??

This strongly limits the programming capability.

Thanks for any help and Best Regards,
Berthold

Subject: Operator overloading in Matlab

From: Doug Schwarz

Date: 4 Nov, 2005 16:11:24

Message: 2 of 6

In article <ef1a649.-1@webx.raydaftYaTP>,
 "Berthold Barzantny" <Berthold.Barzantny@iav.de> wrote:

> Hello everybody,
>
> Matlab has a limited capability of object-oriented programming. This
> includes operator overloading, one of the most important features of
> object-oriented programming.
>
> Does anybody know, why it is not possible to overload the assigning
> operation of two objects like a=b??
>
> This strongly limits the programming capability.
>
> Thanks for any help and Best Regards,
> Berthold


Interesting question. What, specifically, do you want to do?

In MATLAB, an operator really calls a function so an expression like

  A + B

is the same as

  plus(A,B)


If there was a function equivalent for assignment, say, assign,

  A = B

would have to be the same as

  assign(A,B)

but this can't work (in MATLAB anyway) because A does not yet exist!

So, I ask again, what do you really want to do that you can't find a way
of doing now?

--
Doug Schwarz
dmschwarz&urgrad,rochester,edu
Make obvious changes to get real email address.

Subject: Operator overloading in Matlab

From: Berthold Barzantny

Date: 4 Nov, 2005 11:40:41

Message: 3 of 6

Doug Schwarz wrote:
>
>
> In article <ef1a649.-1@webx.raydaftYaTP>,
> "Berthold Barzantny" <Berthold.Barzantny@iav.de> wrote:
>
>> Hello everybody,
>>
>> Matlab has a limited capability of object-oriented programming.
> This
>> includes operator overloading, one of the most important
features
> of
>> object-oriented programming.
>>
>> Does anybody know, why it is not possible to overload the
> assigning
>> operation of two objects like a=b??
>>
>> This strongly limits the programming capability.
>>
>> Thanks for any help and Best Regards,
>> Berthold
>
>
> Interesting question. What, specifically, do you want to do?
>
> In MATLAB, an operator really calls a function so an expression
> like
>
> A + B
>
> is the same as
>
> plus(A,B)
>
>
> If there was a function equivalent for assignment, say, assign,
>
> A = B
>
> would have to be the same as
>
> assign(A,B)
>
> but this can't work (in MATLAB anyway) because A does not yet
> exist!
>
> So, I ask again, what do you really want to do that you can't find
> a way
> of doing now?
>
> --
> Doug Schwarz
> dmschwarz&urgrad,rochester,edu
> Make obvious changes to get real email address.
>
Hello Doug Schwarz,

thanx for your answer.

An expression like

A = B

should lead to a function call like

function A = assign(B)
A = ObjectType % creation of new object
A.member1 = B.member1 % copying members
A.member2 = B.member2
% a.s.o.

in which I can create a new object of type B, copy all members from B
to A and return A.
The "user" of A = B would not realize, which operations are carried
out during copying. Regarding operator overloading this is a common
C++ or Java procedure.

My problem is that I have a member in my objects that should be
unique for all instances like a pointer address.
If I now copy without operator overloading I have the same members in
two objects, which is an inconsistence. A solution would be to use a
special clone function, which is not really perfect, though, since
its usage can easily be forgotten. Furthermore, those inconsistencies
are really hard to detect.
I hope this makes the problem more understandable;-).

Best Regards,
Berthold

Subject: Operator overloading in Matlab

From: us

Date: 4 Nov, 2005 12:01:58

Message: 4 of 6

Berthold Barzantny:
<SNIP wants to clone an object with a new distinct id...

to me, the use of
     OBJ2=OBJ1
to achieve this goal seems quite problematic, eg, what if the user
wants to really(!) simply copy the old object (there are many
situations i can think of)?

we have the same problem and simply use this (admittedly pedestrian)
approach

     OBJ1=objcreator(<input>);
     OBJ2=objcreator(OBJ1);
the first syntax creates a new object based on user defined input
options
the second syntax senses the input,
performs a OBJ2=OBJ1,
and changes the id field

just a thought
us

Subject: Operator overloading in Matlab

From: T Krauss

Date: 5 Nov, 2005 03:55:19

Message: 5 of 6

Berthold Barzantny wrote:
 > Doug Schwarz wrote:
 >
 >>
 >>In article <ef1a649.-1@webx.raydaftYaTP>,
 >>"Berthold Barzantny" <Berthold.Barzantny@iav.de> wrote:
 >>
 >>
 >>>Hello everybody,
 >>>
 >>>Matlab has a limited capability of object-oriented programming.
 >>
 >>This
 >>
 >>>includes operator overloading, one of the most important
 >
 > features
 >
 >>of
 >>
 >>>object-oriented programming.
 >>>
 >>>Does anybody know, why it is not possible to overload the
 >>
 >>assigning
 >>
 >>>operation of two objects like a=b??
 >>>
 >>>This strongly limits the programming capability.
 >>>
 >>>Thanks for any help and Best Regards,
 >>>Berthold
 >>
 >>
 >>Interesting question. What, specifically, do you want to do?
 >>

 >
 > Hello Doug Schwarz,
 >
 > thanx for your answer.
 >
 > An expression like
 >
 > A = B
 >
 > should lead to a function call like
 >
 > function A = assign(B)
 > A = ObjectType % creation of new object
 > A.member1 = B.member1 % copying members
 > A.member2 = B.member2
 > % a.s.o.
 >
 > in which I can create a new object of type B, copy all members from B
 > to A and return A.
 > The "user" of A = B would not realize, which operations are carried
 > out during copying. Regarding operator overloading this is a common
 > C++ or Java procedure.
 >
 > My problem is that I have a member in my objects that should be
 > unique for all instances like a pointer address.
 > If I now copy without operator overloading I have the same members in
 > two objects, which is an inconsistence. A solution would be to use a
 > special clone function, which is not really perfect, though, since
 > its usage can easily be forgotten. Furthermore, those inconsistencies
 > are really hard to detect.
 > I hope this makes the problem more understandable;-).
 >
 > Best Regards,
 > Berthold

Hi Doug, Berthold,

I assume you've got some kind of nested function object with fields that
are function handles to the nested functions. So, the "object" you are
talking about involves copying not just the function handles, but
creating a new "instance" of the nested function object, with "state"
(variables in its workspace) equal to the state of the RHS object.

I don't think there's a way to do this in MATLAB currently. Assignment
gives a copy, plain and simple, not a new object.

If you are willing to change your syntax a bit, you could get this to
happen with a "subsref" method, e.g.
   A = B()
or
   A = B.clone;

Of course, wait a while and MathWorks will release their "new and
improved" object oriented features, which are currently in beta test
(but closed to new beta users). Whatever you end up with will almost
surely be superceded by something better (or so I hope, very much!).

[by the way, been a while since I've posted - good to be back, but
I am out as of tomorrow morning until NEXT friday]

Best regards,
   Tom Krauss

Subject: Operator overloading in Matlab

From: T Krauss

Date: 5 Nov, 2005 04:10:36

Message: 6 of 6

Berthold Barzantny wrote:
> Hello everybody,
>
> Matlab has a limited capability of object-oriented programming. This
> includes operator overloading, one of the most important features of
> object-oriented programming.
>
> Does anybody know, why it is not possible to overload the assigning
> operation of two objects like a=b??
>
> This strongly limits the programming capability.
>
> Thanks for any help and Best Regards,
> Berthold

Berthold -

Don't you hate it when some one responds but doesn't answer your question?

You asked "why" it is not possible, and I proceeded to ignore your
question and instead suggested a way to do something like this (but not
exactly the same syntax).

Sorry.

As to why, well, MATLAB has no references. That is, EVERYTHING is pass
by value. Only very recently have nested function objects (MATLAB 7)
come into being. These allow you to do some object-oriented stuff with
reference-like behavior. But the language (at least publicly to us
outside the MathWorks) as of yet hasn't really caught up... the operator
overloading is a legacy from the old MATLAB 5 style objects, which are
part of the "pass by value" world of MATLAB.

Object oriented programming has been around in MATLAB since version 4,
specifically in the handle-graphics hierarchy. Handles are references
to graphical objects, and you can change them by passing them as inputs
to a function (specifically, "set"). But you still can't add your own
objects, with the exception of nested function objects. But these don't
support operator overloading. But (3rd but) ... you can do some
interesting things combining nested function objects and v5-style
classes, as I suggested in my last post.

Hopefully that at least partially answers your question.

Regards again,
   Tom

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