Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.


EBS
Posts:
85
Registered:
2/11/09


Re: Value classes  do you use them? also syntax questions
Posted:
May 22, 2013 4:32 PM


"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/intendtoextend/ > > 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 :)



