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:
Value classes - do you use them? also syntax questions

Subject: Value classes - do you use them? also syntax questions

From: Eric Sampson

Date: 21 May, 2013 20:22:13

Message: 1 of 5

I posted the below on Answers but got no responses, perhaps the newsgroup is a better forum to discuss this :)
---------

So I can't really recall ever creating a value class, all mine seem to be handle classes. Is that the same as your experience? Unless I'm completely missing something, using value classes seems to be a bit ungainly, because you always have to do two steps like this:

foo = MyValueClass(5); % sets a property 'val'
foo = foo.double();
plot(foo.val);

With a handle class, I'd just do this:

foo = MyHandleClass(5);
plot(foo.double()); % double returns the new obj.val directly.

Am I completely out to lunch with this understanding of value classes?

As an aside, sometimes it seems like it would be nice to have the above syntax of a handle class, but be able to make independent copies of a given object like a value class... Anyone else think so?

Thanks for your thoughts, Eric

Subject: Value classes - do you use them? also syntax questions

From: Steven_Lord

Date: 21 May, 2013 21:50:54

Message: 2 of 5



"Eric Sampson" <ericDOTsampson@gmail.com> wrote in message
news:kngl1l$gd1$1@newscl01ah.mathworks.com...
> I posted the below on Answers but got no responses, perhaps the newsgroup
> is a better forum to discuss this :)
> ---------
>
> So I can't really recall ever creating a value class, all mine seem to be
> handle classes. Is that the same as your experience? Unless I'm completely
> missing something, using value classes seems to be a bit ungainly, because
> you always have to do two steps like this:
>
> foo = MyValueClass(5); % sets a property 'val'
> foo = foo.double();
> plot(foo.val);

Why does the DOUBLE method of the MyValueClass return an object instead of a
double array?

http://www.mathworks.com/help/matlab/matlab_oop/converting-objects-to-another-class.html

> With a handle class, I'd just do this:
>
> foo = MyHandleClass(5);
> plot(foo.double()); % double returns the new obj.val directly.
>
> Am I completely out to lunch with this understanding of value classes?

I suggest you read through this if you haven't already.

http://www.mathworks.com/help/matlab/matlab_oop/comparing-handle-and-value-classes.html

My mental model: if I want to use the object like a MATLAB array, I make it
a value.

A = [1 2 3; 4 5 6];
C = A;
b = sin(A);

If the object is a "pointer" or refers to something with physical meaning
(where it doesn't make sense to "copy" it by doing assignment) I make it a
handle.

fid = myFileObject('c:\temp\temp.txt'); % There is only one temp.txt file in
c:\temp
h = myCompositeGraphicsObject; % h is associated with an object on screen
h2 = h % h2 and h both refer to the same object; there's no copying being
done here

> As an aside, sometimes it seems like it would be nice to have the above
> syntax of a handle class, but be able to make independent copies of a
> given object like a value class... Anyone else think so?

That's what matlab.mixin.Copyable is for.

http://www.mathworks.com/help/matlab/ref/matlab.mixin.copyableclass.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Value classes - do you use them? also syntax questions

From: Eric Sampson

Date: 22 May, 2013 15:42:10

Message: 3 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message <kngq7u$2to$1@newscl01ah.mathworks.com>...
>
>
> "Eric Sampson" <ericDOTsampson@gmail.com> wrote in message
> news:kngl1l$gd1$1@newscl01ah.mathworks.com...
> > I posted the below on Answers but got no responses, perhaps the newsgroup
> > is a better forum to discuss this :)
> > ---------
> >
> > So I can't really recall ever creating a value class, all mine seem to be
> > handle classes. Is that the same as your experience? Unless I'm completely
> > missing something, using value classes seems to be a bit ungainly, because
> > you always have to do two steps like this:
> >
> > foo = MyValueClass(5); % sets a property 'val'
> > foo = foo.double();
> > plot(foo.val);
>
> Why does the DOUBLE method of the MyValueClass return an object instead of a
> double array?
>

Sorry Steve, my example was quickly made and obviously confusing; I was intending the DOUBLE method to just take the current value of the VAL property and multiply it by two, not do a conversion. I didn't even realize that DOUBLE/CHAR/etc methods had special meanings, I should have called my method TIMESTWO to prevent confusion :)

classdef myValueClass

properties
  val = [];
end

methods
  function obj = timestwo(obj, in)
    obj.val = 2 * in;
  end
end

Perhaps the above might explain my original question better now? I can't return the result of TIMESTWO directly instead of the object, because if I did that then the modified object (containing the updated VAL) would not overwrite the original object in the base workspace. I could change the timestwo method to look like this, function [obj, out] = timestwo(in), but then I couldn't call it in a chained function call situation like plot(sin(obj.timestwo(5))) ...

>
> > As an aside, sometimes it seems like it would be nice to have the above
> > syntax of a handle class, but be able to make independent copies of a
> > given object like a value class... Anyone else think so?
>
> That's what matlab.mixin.Copyable is for.
>
> http://www.mathworks.com/help/matlab/ref/matlab.mixin.copyableclass.html
>

Thanks Steve, I'll confess that I hadn't figured out the purpose of that abstract class yet! I think the fact that it was in the 'mixin' package threw me off, compared to the other abstract handle classes like hgsetget/dynamicprops which are not - I guess I figured from that fact & the package name that it had to do with defining a mixin class.

Speaking of that, how do you do mixins in MATLAB OOP? Also, how/can you add methods dynamically to a class and/or instance? http://www.medihack.org/2011/03/15/intend-to-extend/

Cheers

Subject: Value classes - do you use them? also syntax questions

From: Steven_Lord

Date: 22 May, 2013 19:32:38

Message: 4 of 5



"Eric Sampson" <ericDOTsampson@gmail.com> wrote in message
news:knip0i$q0c$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <kngq7u$2to$1@newscl01ah.mathworks.com>...
>>
>>
>> "Eric Sampson" <ericDOTsampson@gmail.com> wrote in message
>> news:kngl1l$gd1$1@newscl01ah.mathworks.com...
>> > I posted the below on Answers but got no responses, perhaps the
>> > newsgroup is a better forum to discuss this :)
>> > ---------
>> >
>> > So I can't really recall ever creating a value class, all mine seem to
>> > be handle classes. Is that the same as your experience? Unless I'm
>> > completely missing something, using value classes seems to be a bit
>> > ungainly, because you always have to do two steps like this:
>> >
>> > foo = MyValueClass(5); % sets a property 'val'
>> > foo = foo.double();
>> > plot(foo.val);
>>
>> Why does the DOUBLE method of the MyValueClass return an object instead
>> of a double array?
>>
>
> Sorry Steve, my example was quickly made and obviously confusing; I was
> intending the DOUBLE method to just take the current value of the VAL
> property and multiply it by two, not do a conversion. I didn't even
> realize that DOUBLE/CHAR/etc methods had special meanings, I should have
> called my method TIMESTWO to prevent confusion :)
>
> classdef myValueClass
>
> properties
> val = [];
> end
>
> methods
> function obj = timestwo(obj, in)
> obj.val = 2 * in;
> end
> end
>
> Perhaps the above might explain my original question better now? I can't
> return the result of TIMESTWO directly instead of the object, because if I
> did that then the modified object (containing the updated VAL) would not
> overwrite the original object in the base workspace. I could change the
> timestwo method to look like this, function [obj, out] = timestwo(in), but
> then I couldn't call it in a chained function call situation like
> plot(sin(obj.timestwo(5))) ...

Try:

classdef myValueClass
    properties
        val = [];
    end
    methods
        function obj = timestwo(obj, in)
            obj.val = 2 * in;
        end
        function v = double(obj)
            v = obj.val;
        end
    end
end

y = myValueClass
y = timestwo(y, 5)
plot(y, 20, 'Marker', 'o', 'MarkerSize', 25)

You should see a circle at x = 10, y = 20. PLOT will try to call y's DOUBLE
method to convert it to a double and use the double precision result to
plot. You can see this by trying to PLOT something that can't be converted
into a double array, like:

plot({1, 2}, 5)

MATLAB doesn't know how to convert a cell array into a double, so it errors.

>> > As an aside, sometimes it seems like it would be nice to have the above
>> > syntax of a handle class, but be able to make independent copies of a
>> > given object like a value class... Anyone else think so?
>>
>> That's what matlab.mixin.Copyable is for.
>>
>> http://www.mathworks.com/help/matlab/ref/matlab.mixin.copyableclass.html
>>
>
> Thanks Steve, I'll confess that I hadn't figured out the purpose of that
> abstract class yet! I think the fact that it was in the 'mixin' package
> threw me off, compared to the other abstract handle classes like
> hgsetget/dynamicprops which are not - I guess I figured from that fact &
> the package name that it had to do with defining a mixin class.

"mixin" is an object-oriented programming term, not something MathWorks
invented.

http://en.wikipedia.org/wiki/Mixin

If hgsetget and dynamicprops were introduced today, I suspect they might be
named matlab.mixin.hgsetget and matlab.mixin.dynamicprops or something
similar.

> Speaking of that, how do you do mixins in MATLAB OOP? Also, how/can you
> add methods dynamically to a class and/or instance?
> http://www.medihack.org/2011/03/15/intend-to-extend/

Some of the techniques from that article are possible in MATLAB. You can
inherit (which is how you'd use the matlab.mixin.* classes.) You can kind of
simulate adding a "method" to an instance by using a property to store a
function handle instead.

% begin myValueClass.m
classdef myValueClass

    properties
        val = [];
    end
    properties(Hidden)
        todo = @dummy;
    end

    methods
        function obj = timestwo(obj, in)
            obj.val = 2 * in;
        end
        function v = double(obj)
            v = obj.val;
        end
    end
end

function varargout = dummy(~, varargin)
[varargout{1:nargout}] = deal([]);
end
% end myValueClass.m

>> y = myValueClass;
>> y = y.timestwo(pi);
>> y.todo()
>> y.todo = @why;
>> y.todo()
To fool the tall good and smart system manager.

Is that a _good_ approach? Maybe not. But it is _an_ approach. Some of the
other approaches may also be possible; I didn't read through the whole list
in detail.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Value classes - do you use them? also syntax questions

From: Eric Sampson

Date: 22 May, 2013 20:32:10

Message: 5 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message <knj6gl$fcs$1@newscl01ah.mathworks.com>...
>
>
(snip)
>
> Try:
>
> classdef myValueClass
> properties
> val = [];
> end
> methods
> function obj = timestwo(obj, in)
> obj.val = 2 * in;
> end
> function v = double(obj)
> v = obj.val;
> end
> end
> end
>
> y = myValueClass
> y = timestwo(y, 5)
> plot(y, 20, 'Marker', 'o', 'MarkerSize', 25)
>
> You should see a circle at x = 10, y = 20. PLOT will try to call y's DOUBLE
> method to convert it to a double and use the double precision result to
> plot. You can see this by trying to PLOT something that can't be converted
> into a double array, like:
>
> plot({1, 2}, 5)
>
> MATLAB doesn't know how to convert a cell array into a double, so it errors.
>

Thanks Steve, interesting approach, I wouldn't have considered that! I appreciate the help :)

> > Speaking of that, how do you do mixins in MATLAB OOP? Also, how/can you
> > add methods dynamically to a class and/or instance?
> > http://www.medihack.org/2011/03/15/intend-to-extend/
>
> Some of the techniques from that article are possible in MATLAB. You can
> inherit (which is how you'd use the matlab.mixin.* classes.) You can kind of
> simulate adding a "method" to an instance by using a property to store a
> function handle instead.
>
(snip)
>
> Is that a _good_ approach? Maybe not. But it is _an_ approach. Some of the
> other approaches may also be possible; I didn't read through the whole list
> in detail.
>

Thanks again Steve, good food for thought. The difference in the Ruby example is that the class doesn't have to do anything special to allow this to work (like adding the extra handle property in your example). To achieve something like this in MATLAB I suppose that you could add a dynamicprop function handle to any handle class that inherits from dynamicprops, and then use that :)

Tags for 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