The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Value classes - do you use them? also syntax questions
Replies: 4   Last Post: May 22, 2013 4:32 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 95
Registered: 2/11/09
Re: Value classes - do you use them? also syntax questions
Posted: May 22, 2013 4:32 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Steven_Lord" <> wrote in message <knj6gl$fcs$>...

> 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?
> >

> 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.

> 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 :)

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.