Thread Subject: matlab oop (beginner's question)

Subject: matlab oop (beginner's question)

From: david cottrell

Date: 17 Dec, 2009 15:54:06

Message: 1 of 4

I'm confused as to what is the best way to implement an value object (not handle) that is mutable.

For example, define a simple "experiment" object that has various input and output parameters and methods that perform the experiment (modifying internal output variables) and compute and plot things.

A handle class basically does this but is a handle so creating independent copies is more inolved:

classdef experiment < handle
 ...
end

Creating a function that returns a structure with handles to all the necessary variables and subroutines will do the trick I think:

function p = experiment
  p.param1=3;
  p.method1 = @f

  function f
      do things to p.param1;
  end
end

but is the best way? It doesn't seem to be using Matlab's newer oop features.

I believe with C++ classes this would be standard behaviour of a class with the default copy constructor.

Subject: matlab oop (beginner's question)

From: Matt J

Date: 17 Dec, 2009 16:55:26

Message: 2 of 4

"david cottrell" <david.cottrell@gmail.com> wrote in message <hgdk6u$skr$1@fred.mathworks.com>...
> I'm confused as to what is the best way to implement an value object (not handle) that is mutable.
>
> For example, define a simple "experiment" object that has various input and output parameters and methods that perform the experiment (modifying internal output variables) and compute and plot things.
>
> A handle class basically does this but is a handle so creating independent copies is more inolved:
>
> classdef experiment < handle
> ...
> end
>
> Creating a function that returns a structure with handles to all the necessary variables and subroutines will do the trick I think:

No, it won't. Consider the following implementation of your idea


function p = experiment
  p.param1=3;
  p.method1 = @f;

  function f
      p.param1=rand(1000,1000);
  end
end

>> z=experiment

z =

     param1: 3
    method1: @experiment/f

>> z.method1()
>> z,

z =

     param1: 3
    method1: @experiment/f


So you see, z.method1() failed to change z.param1 as was your intention.

The real result of z.method1() is sitting somewhere inside the function handle:

>> T=functions(z.method1); T=T.workspace{1}; T=T.p

T =

     param1: [1000x1000 double]
    method1: @experiment/f


Another disadvantage of this approach is that, the memory consumption of z attributed to the function handle z.method1 is invisible, as you can see from the following,

>> whos z T
  Name Size Bytes Class Attributes

  T 1x1 8000388 struct
  z 1x1 272 struct


Still, it can be a useful quick and dirty way of obtaining oop features at times. I use it occasionally.

Subject: matlab oop (beginner's question)

From: Steven Lord

Date: 17 Dec, 2009 18:35:13

Message: 3 of 4


"david cottrell" <david.cottrell@gmail.com> wrote in message
news:hgdk6u$skr$1@fred.mathworks.com...
> I'm confused as to what is the best way to implement an value object (not
> handle) that is mutable.

Have ALL the methods that modify the object return the modified object, and
ALWAYS call the method with an output argument that will contain the
modified object.

A value object behaves in much the same way as a regular MATLAB matrix. If
you pass a matrix into a function to be modified, you MUST return it from
the function in order to "see" the changes to it.

Now while there _are_ ways to avoid this requirement, you asked for "the
best way" to implement that change -- and the above is IMO the best way.

> For example, define a simple "experiment" object that has various input
> and output parameters and methods that perform the experiment (modifying
> internal output variables) and compute and plot things.
>
> A handle class basically does this but is a handle so creating independent
> copies is more inolved:

True. But if you need or want the methods of your class to make a change
and have those changes affect the copy of the object in the workspace from
which the method was called, that's the defining characteristic of a handle
class, and so you should make your class a handle class.

If you're looking for a pet that can bark, which is more natural -- getting
a dog or getting a cat and training it how to bark?

*snip*

> I believe with C++ classes this would be standard behaviour of a class
> with the default copy constructor.

What does isequal('MATLAB', 'C++') return?

MATLAB classes and C++ classes can (and do) behave differently sometimes.
Neither is wrong; they're just different.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: matlab oop (beginner's question)

From: PinkLab

Date: 17 Dec, 2009 18:56:24

Message: 4 of 4

You need to go on www confused com website

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
oop d c 17 Dec, 2009 10:59:09
handle vs value d c 17 Dec, 2009 10:59:09
classes d c 17 Dec, 2009 10:59:09
rssFeed for this Thread

Contact us at files@mathworks.com