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:
Default object output (object oriented programming)

Subject: Default object output (object oriented programming)

From: R Wat

Date: 29 Nov, 2011 21:40:08

Message: 1 of 18

I am relatively new to the world of object oriented programming, so sorry if I don't use the proper terminology.

Lets say I have the following class definition

classdef TestObject
    properties
        Data; % An vector of data
        Units; % The corresponding units of the data
    end

    methods
        function obj = TestObject(data,units)
             obj.Data = data;
             obj.Units = units;
        end
    end
end

Lets say I have 2 instances of my TestObject class:

testobject1 = TestObject(1:20,'units');
testobject2 = TestObject(sin([1:20]),'units');

The majority of the time that I use an instance of the class TestObject, I am calling the Data property. So, in this case, I do a lot of things like: plot(testobject1.Data,testobject2.Data).

Is there a way to make Data the default property of the TestObject class? By this, is there a way to setup the class definition such, when I call a TestObject by itself, it outputs the Data property? So, instead of plot(testobject1.Data,testobject2.Data), I could just do something like plot(testobject1,testobject2) where here it is just automatically assumed that I want the Data property as nothing is specified. I have tried making a display property for the class, but that simply does what is say; displays the results but can not be used in the sense I described above.

Does anyone know of a way to implement this?

This is not a deal breaker, but would just making object calls less cumbersome. Thanks for any suggestions you may have!

Subject: Default object output (object oriented programming)

From: Rune Allnor

Date: 29 Nov, 2011 22:04:07

Message: 2 of 18

On 29 Nov, 22:40, "R Wat" <rhino7...@aol.com> wrote:

> The majority of the time that I use an instance of the class TestObject, I am calling the Data property. So, in this case, I do a lot of things like: plot(testobject1.Data,testobject2.Data).
>
> Is there a way to make Data the default property of the TestObject class? By this, is there a way to setup the class definition such, when I call a TestObject by itself, it outputs the Data property? So, instead of plot(testobject1.Data,testobject2.Data), I could just do something like plot(testobject1,testobject2) where here it is just automatically assumed that I want the Data property as nothing is specified.

The short answer is: No.

The longer answeris that if this was possible,
it would be impossible, both for the human reader
and the computer, to determine what a statement
like

x = testobject;

will mean: Copy the whole object? Or just this
default property you want?

So you will have to write a litte bit more code
to ensure that what you write makes sense.

Rune

Subject: Default object output (object oriented programming)

From: ade77

Date: 29 Nov, 2011 22:12:08

Message: 3 of 18

There are many options, some of them might diminish the objective of using OOP in the first instance. One good option is just create another function to do the plot in your class def.
In your example, like this:

classdef TestObject
     properties
         Data; % An vector of data
         Units; % The corresponding units of the data
     end
 
     methods
         function obj = TestObject(data,units)
              obj.Data = data;
              obj.Units = units;
         end
             
         function plot_data( x, y)
             plot(x.Data, y.Data)
         end
     
     end
 end

> testobject1 = TestObject(1:20,'units');
> testobject2 = TestObject(sin([1:20]),'units');
> plot_data(testobject1, testobject2).

This will be the most natural approach, since u can customize the function(plot_data) to include many things.

BTW.... if u are new to OOP, I would advise u learn OOP from JAVA or other low level language to give u a good foundation on OOP (JAVA is number 1). By learning OOP only from MATLAB do not think u can confidently tell people u understand OOP. MATLAB OOP is not very matured.... just my opinion

Also, the fact that u have something written in "class" does not mean it is OOP

Subject: Default object output (object oriented programming)

From: Doug Schwarz

Date: 30 Nov, 2011 17:22:44

Message: 4 of 18

On 11/29/2011 4:40 PM, R Wat wrote:
> I am relatively new to the world of object oriented programming, so
> sorry if I don't use the proper terminology.
> Lets say I have the following class definition
>
> classdef TestObject
> properties
> Data; % An vector of data
> Units; % The corresponding units of the data
> end
>
> methods
> function obj = TestObject(data,units)
> obj.Data = data;
> obj.Units = units;
> end
> end
> end
>
> Lets say I have 2 instances of my TestObject class:
> testobject1 = TestObject(1:20,'units');
> testobject2 = TestObject(sin([1:20]),'units');
>
> The majority of the time that I use an instance of the class TestObject,
> I am calling the Data property. So, in this case, I do a lot of things
> like: plot(testobject1.Data,testobject2.Data).
>
> Is there a way to make Data the default property of the TestObject
> class? By this, is there a way to setup the class definition such, when
> I call a TestObject by itself, it outputs the Data property? So, instead
> of plot(testobject1.Data,testobject2.Data), I could just do something
> like plot(testobject1,testobject2) where here it is just automatically
> assumed that I want the Data property as nothing is specified. I have
> tried making a display property for the class, but that simply does what
> is say; displays the results but can not be used in the sense I
> described above.
>
> Does anyone know of a way to implement this?

Yes, simply make TestObject a subclass of double:

classdef TestObject < double

     properties
         Units
     end

     methods
         function obj = TestObject(data,units)
             obj = obj@double(data);
             obj.Units = units;
         end
     end
end

Use it like this:

 >> q = TestObject([1 5 3],'units');
 >> plot(q)

A copy operation will copy the object, but any arithmetic operation that
expects a double will use the value you passed in.


--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Default object output (object oriented programming)

From: R Wat

Date: 30 Nov, 2011 19:55:10

Message: 5 of 18

Doug Schwarz <see@sig.for.address.edu> wrote in message <EDtBq.592$8O4.36@newsfe16.iad>...
> On 11/29/2011 4:40 PM, R Wat wrote:
>
> Yes, simply make TestObject a subclass of double:
>
> classdef TestObject < double
>
> properties
> Units
> end
>
> methods
> function obj = TestObject(data,units)
> obj = obj@double(data);
> obj.Units = units;
> end
> end
> end
>
> Use it like this:
>
> >> q = TestObject([1 5 3],'units');
> >> plot(q)
>
> A copy operation will copy the object, but any arithmetic operation that
> expects a double will use the value you passed in.
>
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Classes like double and integer where what originally made me think there was a possibility that I could do this. I never thought to subclass them though.

Thanks a lot!

Subject: Default object output (object oriented programming)

From: per isakson

Date: 30 Nov, 2011 19:55:10

Message: 6 of 18

Doug Schwarz <see@sig.for.address.edu> wrote in message <EDtBq.592$8O4.36@newsfe16.iad>...
[...]
> classdef TestObject < double
>
> properties
> Units
> end
>
> methods
> function obj = TestObject(data,units)
> obj = obj@double(data);
> obj.Units = units;
> end
> end
> end
>
> Use it like this:
>
> >> q = TestObject([1 5 3],'units');
> >> plot(q)
>
> A copy operation will copy the object, but any arithmetic operation that
> expects a double will use the value you passed in.
[...]

This is clever, howerver isn't it the start of a huge undertaking?

Visual Basic has a "default property". I tested that idea with a couple of classes. I used subref. A couple of month later I refactored the classes to get rid of the "default property". The reason was that things happened that I had not anticipated - side effects. And my documentation was not good enough. Conclusion, KISS applies to me!

a = q; % fine
b = q+q; % returns a double. Do I want that?

The class timeseries in Matlab is not a subclass of double for some reason. The timeseries has "every" operation overloaded.

KISS in this case would be to overload plot in TestObject.

BTW: Matlab is a dynamic language. Doesn't it make more sense to pick up a Python book on OOP?

 - per
 

  

Subject: Default object output (object oriented programming)

From: R Wat

Date: 30 Nov, 2011 20:37:09

Message: 7 of 18

"per isakson" wrote in message <jb61mu$sgq$1@newscl01ah.mathworks.com>...
> [...]
>
> This is clever, howerver isn't it the start of a huge undertaking?
>
> Visual Basic has a "default property". I tested that idea with a couple of classes. I used subref. A couple of month later I refactored the classes to get rid of the "default property". The reason was that things happened that I had not anticipated - side effects. And my documentation was not good enough. Conclusion, KISS applies to me!
>
> a = q; % fine
> b = q+q; % returns a double. Do I want that?
>
> The class timeseries in Matlab is not a subclass of double for some reason. The timeseries has "every" operation overloaded.
>
> KISS in this case would be to overload plot in TestObject.
>

After looking into my situation a little more, I agree completely with you. Originally, I underestimated the potential of overloading the plot function, which I have since done (and it works beautifully). My plot function found in the class definition of TestObject looks something like this:

    function varargout = plot(varargin)
            % PLOT plots TestObject Data property
            % PLOT(OBJ) plots the 'Data' property found in the
            % TestObject object OBJ with respect to its data index.
            % This is comparable to matlab's plot(x) where x is a vector.
            %
            % PLOT(OBJ1,OBJ2) plots the Data property field of OBJ2 vs
            % the Data property field of OBJ1. This is comprable to
            % Matlab's plot(x,y) where x and y are vectors of the same
            % length.
            %
            % PLOT(X,OBJ) plots the Data property of OBJ with respect to
            % the vector X such that the length of X and the Data
            % property of OBJ are the same length.
            %
            % PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) plots the data Ith
            % data YI vs XI, which can be either a vector or
            % TestObject object, with LineSpec SI.
            %
            % H = PLOT(...) outputs the plotted line data handles.
            %
            % You can also plot multiple lines on the same plot as you
            % would using Matlabs plotting function along with LineSpecs.
            % So, for example, you could so the following:
            % x = 1:20;
            % obj1 = AcquisitionData(sin(x));
            % obj2 = AcquisitionData(sin(x).^2);
            % y3 = cos(x);
            % plot(x,obj1,'r--',x,obj2,'bx',x,y)
            %
            % See also: plot

            % Loop through all of the input arguments. If one of them is a
            % TestObject object, change the varargin of the current index to its
            % corresponding Data property field. Otherwise, leave the data alone
            for ii = 1:length(varargin)
                if isa(varargin{ii},'AcquisitionData')
                    varargin{ii} = varargin{ii}.Data;
                end
            end
            
            % Plot the data as you normally would in matlab
            hL = plot(varargin{:});
            
            % Output plottend data handles if specified
            if nargout > 0
                varargout{1} = hL;
            end
        end

Thanks again everyone!

Subject: Default object output (object oriented programming)

From: Richard Ellis

Date: 30 Nov, 2011 23:07:21

Message: 8 of 18

I think the best solution is to implement a double converter. MATLAB calls
the double method of an object, if it exists, when the MATLAB function
expects a double (or other type). Try this:

classdef TestObject
   properties
      Data; % An vector of data
      Units; % The corresponding units of the data
   end

   methods
      function obj = TestObject(data,units)
         obj.Data = data;
         obj.Units = units;
      end
      function db = double(obj)
         db = obj.Data;
      end
   end
end

Then pass an object to plot:

>> o = TestObject(1:10,'inches');
>> plot(o)

"R Wat" <rhino7890@aol.com> wrote in message
news:jb61mt$sgl$1@newscl01ah.mathworks.com...
> Doug Schwarz <see@sig.for.address.edu> wrote in message
> <EDtBq.592$8O4.36@newsfe16.iad>...
>> On 11/29/2011 4:40 PM, R Wat wrote:
>>
>> Yes, simply make TestObject a subclass of double:
>>
>> classdef TestObject < double
>>
>> properties
>> Units
>> end
>>
>> methods
>> function obj = TestObject(data,units)
>> obj = obj@double(data);
>> obj.Units = units;
>> end
>> end
>> end
>>
>> Use it like this:
>>
>> >> q = TestObject([1 5 3],'units');
>> >> plot(q)
>>
>> A copy operation will copy the object, but any arithmetic operation that
>> expects a double will use the value you passed in.
>>
>>
>> --
>> Doug Schwarz
>> dmschwarz&ieee,org
>> Make obvious changes to get real email address.
>
> Classes like double and integer where what originally made me think there
> was a possibility that I could do this. I never thought to subclass them
> though.
> Thanks a lot!

Subject: Default object output (object oriented programming)

From: per isakson

Date: 1 Dec, 2011 12:55:09

Message: 9 of 18

"Richard Ellis" <rich@mathworks.com> wrote in message <jb6cvc$8ig$1@newscl01ah.mathworks.com>...
> I think the best solution is to implement a double converter. MATLAB calls
> the double method of an object, if it exists, when the MATLAB function
> expects a double (or other type). Try this:
>
> classdef TestObject
> properties
> Data; % An vector of data
> Units; % The corresponding units of the data
> end
>
> methods
> function obj = TestObject(data,units)
> obj.Data = data;
> obj.Units = units;
> end
> function db = double(obj)
> db = obj.Data;
> end
> end
> end
>
> Then pass an object to plot:
>
> >> o = TestObject(1:10,'inches');
> >> plot(o)
>
[...]

This looks useful. However,
1. How do I learn which "MATLAB function expects a double (or other type)"? PLOT is built-in
2. Is it possible to make an m-file-function expect double (or other type)?

- per
 

Subject: Default object output (object oriented programming)

From: Rune Allnor

Date: 1 Dec, 2011 13:05:52

Message: 10 of 18

On 1 Des, 13:55, "per isakson" <p...@bim.kth.se> wrote:
> "Richard Ellis" <r...@mathworks.com> wrote in message <jb6cvc$8i...@newscl01ah.mathworks.com>...
> > I think the best solution is to implement a double converter. MATLAB calls
> > the double method of an object, if it exists, when the MATLAB function
> > expects a double (or other type). Try this:
>
> > classdef TestObject
> >    properties
> >       Data;    % An vector of data
> >       Units;   % The corresponding units of the data
> >    end
>
> >    methods
> >       function obj = TestObject(data,units)
> >          obj.Data = data;
> >          obj.Units = units;
> >       end
> >       function db = double(obj)
> >          db = obj.Data;
> >       end
> >    end
> > end
>
> > Then pass an object to plot:
>
> > >>  o = TestObject(1:10,'inches');
> > >> plot(o)
>
> [...]
>
> This looks useful. However,
> 1. How do I learn which "MATLAB function expects a double (or other type)"? PLOT is built-in
> 2. Is it possible to make an m-file-function expect double (or other type)?

Not in the sense you might expect if you have
experience with other programming languages that
use argument type matching in function calls.

You need to test the arguments explicitly for
type:

doc ischar
doc isnumeric
doc iscell
doc isstruct

and so on.

Rune

Subject: Default object output (object oriented programming)

From: per isakson

Date: 1 Dec, 2011 14:29:09

Message: 11 of 18

Rune Allnor <allnor@tele.ntnu.no> wrote in message <db89ac1c-44b4-4bc3-a569-d5946509f08f@n6g2000vbg.googlegroups.com>...
> On 1 Des, 13:55, "per isakson" <p...@bim.kth.se> wrote:
> > "Richard Ellis" <r...@mathworks.com> wrote in message <jb6cvc$8i...@newscl01ah.mathworks.com>...
> > > I think the best solution is to implement a double converter. MATLAB calls
> > > the double method of an object, if it exists, when the MATLAB function
> > > expects a double (or other type). Try this:
> >
> > > classdef TestObject
> > >    properties
> > >       Data;    % An vector of data
> > >       Units;   % The corresponding units of the data
> > >    end
> >
> > >    methods
> > >       function obj = TestObject(data,units)
> > >          obj.Data = data;
> > >          obj.Units = units;
> > >       end
> > >       function db = double(obj)
> > >          db = obj.Data;
> > >       end
> > >    end
> > > end
> >
> > > Then pass an object to plot:
> >
> > > >>  o = TestObject(1:10,'inches');
> > > >> plot(o)
> >
> > [...]
> >
> > This looks useful. However,
> > 1. How do I learn which "MATLAB function expects a double (or other type)"? PLOT is built-in
> > 2. Is it possible to make an m-file-function expect double (or other type)?
>
> Not in the sense you might expect if you have
> experience with other programming languages that
> use argument type matching in function calls.
>
> You need to test the arguments explicitly for
> type:
>
> doc ischar
> doc isnumeric
> doc iscell
> doc isstruct
>
> and so on.
>
> Rune

I think know Matlab fairly well, but Richard Ellis' example above puzzles me. It works ok, but I cannot figure out which function, besides PLOT, will make an "implicit" call to the overloaded method, double. For example "hist(o)" doesn't work.
- per

Subject: Default object output (object oriented programming)

From: Rune Allnor

Date: 1 Dec, 2011 14:37:48

Message: 12 of 18

On 1 Des, 15:29, "per isakson" <p...@bim.kth.se> wrote:
> Rune Allnor <all...@tele.ntnu.no> wrote in message <db89ac1c-44b4-4bc3-a569-d5946509f...@n6g2000vbg.googlegroups.com>...
> > On 1 Des, 13:55, "per isakson" <p...@bim.kth.se> wrote:
> > > "Richard Ellis" <r...@mathworks.com> wrote in message <jb6cvc$8i...@newscl01ah.mathworks.com>...
> > > > I think the best solution is to implement a double converter. MATLAB calls
> > > > the double method of an object, if it exists, when the MATLAB function
> > > > expects a double (or other type). Try this:
>
> > > > classdef TestObject
> > > >    properties
> > > >       Data;    % An vector of data
> > > >       Units;   % The corresponding units of the data
> > > >    end
>
> > > >    methods
> > > >       function obj = TestObject(data,units)
> > > >          obj.Data = data;
> > > >          obj.Units = units;
> > > >       end
> > > >       function db = double(obj)
> > > >          db = obj.Data;
> > > >       end
> > > >    end
> > > > end
>
> > > > Then pass an object to plot:
>
> > > > >>  o = TestObject(1:10,'inches');
> > > > >> plot(o)
>
> > > [...]
>
> > > This looks useful. However,
> > > 1. How do I learn which "MATLAB function expects a double (or other type)"? PLOT is built-in
> > > 2. Is it possible to make an m-file-function expect double (or other type)?
>
> > Not in the sense you might expect if you have
> > experience with other programming languages that
> > use argument type matching in function calls.
>
> > You need to test the arguments explicitly for
> > type:
>
> > doc ischar
> > doc isnumeric
> > doc iscell
> > doc isstruct
>
> > and so on.
>
> > Rune
>
> I think know Matlab fairly well, but Richard Ellis' example above puzzles me. It works ok, but I cannot figure out which function, besides PLOT, will make an "implicit" call to the overloaded method, double. For example "hist(o)" doesn't work.

I must admit that I don't see why that method should work
at all. But then, I prefer to use C++ for the 'serious'
work and haven't spent much time trying to sort out
the various idiosyncracies of matlab.

I have noted that in C++ there has been spent a lot
of effort on strict rules for both type and operator
overloading, so that behaviour should be predictable
and consistent. As not nearly as much effort has been
spent on the design of the matlab OO system, there is
some possibility that we are now discussing undefined
behaviour. That is, a case that was never adressed in
the spec, and which behaves differently between cases
which seem smilar to the reader.

Rune

Subject: Default object output (object oriented programming)

From: per isakson

Date: 1 Dec, 2011 15:03:08

Message: 13 of 18

Rune Allnor <allnor@tele.ntnu.no> wrote in message <864319a8-ef1c-4616-9154-96861a7870a8@h5g2000yqk.googlegroups.com>...
> On 1 Des, 15:29, "per isakson" <p...@bim.kth.se> wrote:
> > Rune Allnor <all...@tele.ntnu.no> wrote in message <db89ac1c-44b4-4bc3-a569-d5946509f...@n6g2000vbg.googlegroups.com>...
> > > On 1 Des, 13:55, "per isakson" <p...@bim.kth.se> wrote:
> > > > "Richard Ellis" <r...@mathworks.com> wrote in message <jb6cvc$8i...@newscl01ah.mathworks.com>...
> > > > > I think the best solution is to implement a double converter. MATLAB calls
> > > > > the double method of an object, if it exists, when the MATLAB function
> > > > > expects a double (or other type). Try this:
> >
> > > > > classdef TestObject
> > > > >    properties
> > > > >       Data;    % An vector of data
> > > > >       Units;   % The corresponding units of the data
> > > > >    end
> >
> > > > >    methods
> > > > >       function obj = TestObject(data,units)
> > > > >          obj.Data = data;
> > > > >          obj.Units = units;
> > > > >       end
> > > > >       function db = double(obj)
> > > > >          db = obj.Data;
> > > > >       end
> > > > >    end
> > > > > end
> >
> > > > > Then pass an object to plot:
> >
> > > > > >>  o = TestObject(1:10,'inches');
> > > > > >> plot(o)
> >
> > > > [...]
> >
> > > > This looks useful. However,
> > > > 1. How do I learn which "MATLAB function expects a double (or other type)"? PLOT is built-in
> > > > 2. Is it possible to make an m-file-function expect double (or other type)?
> >
> > > Not in the sense you might expect if you have
> > > experience with other programming languages that
> > > use argument type matching in function calls.
> >
> > > You need to test the arguments explicitly for
> > > type:
> >
> > > doc ischar
> > > doc isnumeric
> > > doc iscell
> > > doc isstruct
> >
> > > and so on.
> >
> > > Rune
> >
> > I think know Matlab fairly well, but Richard Ellis' example above puzzles me. It works ok, but I cannot figure out which function, besides PLOT, will make an "implicit" call to the overloaded method, double. For example "hist(o)" doesn't work.
>
> I must admit that I don't see why that method should work
> at all. But then, I prefer to use C++ for the 'serious'
> work and haven't spent much time trying to sort out
> the various idiosyncracies of matlab.
>
> I have noted that in C++ there has been spent a lot
> of effort on strict rules for both type and operator
> overloading, so that behaviour should be predictable
> and consistent. As not nearly as much effort has been
> spent on the design of the matlab OO system, there is
> some possibility that we are now discussing undefined
> behaviour. That is, a case that was never adressed in
> the spec, and which behaves differently between cases
> which seem smilar to the reader.
>
> Rune

I believe it indeed is a defined behaviour since Richard Ellis is with The Mathworks. His email address says so.
- per

Subject: Default object output (object oriented programming)

From: Rune Allnor

Date: 1 Dec, 2011 15:13:48

Message: 14 of 18

On 1 Des, 16:03, "per isakson" <p...@bim.kth.se> wrote:
> Rune Allnor <all...@tele.ntnu.no> wrote in message <864319a8-ef1c-4616-9154-96861a787...@h5g2000yqk.googlegroups.com>...
> > On 1 Des, 15:29, "per isakson" <p...@bim.kth.se> wrote:
> > > Rune Allnor <all...@tele.ntnu.no> wrote in message <db89ac1c-44b4-4bc3-a569-d5946509f...@n6g2000vbg.googlegroups.com>...
> > > > On 1 Des, 13:55, "per isakson" <p...@bim.kth.se> wrote:
> > > > > "Richard Ellis" <r...@mathworks.com> wrote in message <jb6cvc$8i...@newscl01ah.mathworks.com>...
> > > > > > I think the best solution is to implement a double converter. MATLAB calls
> > > > > > the double method of an object, if it exists, when the MATLAB function
> > > > > > expects a double (or other type). Try this:
>
> > > > > > classdef TestObject
> > > > > >    properties
> > > > > >       Data;    % An vector of data
> > > > > >       Units;   % The corresponding units of the data
> > > > > >    end
>
> > > > > >    methods
> > > > > >       function obj = TestObject(data,units)
> > > > > >          obj.Data = data;
> > > > > >          obj.Units = units;
> > > > > >       end
> > > > > >       function db = double(obj)
> > > > > >          db = obj.Data;
> > > > > >       end
> > > > > >    end
> > > > > > end
>
> > > > > > Then pass an object to plot:
>
> > > > > > >>  o = TestObject(1:10,'inches');
> > > > > > >> plot(o)
>
> > > > > [...]
>
> > > > > This looks useful. However,
> > > > > 1. How do I learn which "MATLAB function expects a double (or other type)"? PLOT is built-in
> > > > > 2. Is it possible to make an m-file-function expect double (or other type)?
>
> > > > Not in the sense you might expect if you have
> > > > experience with other programming languages that
> > > > use argument type matching in function calls.
>
> > > > You need to test the arguments explicitly for
> > > > type:
>
> > > > doc ischar
> > > > doc isnumeric
> > > > doc iscell
> > > > doc isstruct
>
> > > > and so on.
>
> > > > Rune
>
> > > I think know Matlab fairly well, but Richard Ellis' example above puzzles me. It works ok, but I cannot figure out which function, besides PLOT, will make an "implicit" call to the overloaded method, double. For example "hist(o)" doesn't work.
>
> > I must admit that I don't see why that method should work
> > at all. But then, I prefer to use C++ for the 'serious'
> > work and haven't spent much time trying to sort out
> > the various idiosyncracies of matlab.
>
> > I have noted that in C++ there has been spent a lot
> > of effort on strict rules for both type and operator
> > overloading, so that behaviour should be predictable
> > and consistent. As not nearly as much effort has been
> > spent on the design of the matlab OO system, there is
> > some possibility that we are now discussing undefined
> > behaviour. That is, a case that was never adressed in
> > the spec, and which behaves differently between cases
> > which seem smilar to the reader.
>
> > Rune
>
> I believe it indeed is a defined behaviour since Richard Ellis is with The Mathworks.

That's not a very convincing way of arguing. Cite the
specification of matlab's OO sytem, if you have access
to it. I suspect few if any have, outside TMW. And I
suspect very few have, even inside TMW.

Apart from that, there are a couple of details that
make me think this is not at all well-defined:

- There is nothing in the docs to show how to do this
  kind of thing.
- The trick, if that's what it is, works in some cases
  and not in other, semantically similar ones.
- The general idea is not at all a good one.

The conclusion is simple: Don't attempt to use that
kind of functionality. Write well-defined code first
time around.

Rune

Subject: Default object output (object oriented programming)

From: Rune Allnor

Date: 1 Dec, 2011 16:13:27

Message: 15 of 18

On 1 Des, 16:13, Rune Allnor <all...@tele.ntnu.no> wrote:

> - The general idea is not at all a good one.

> Write well-defined code first
> time around.

Just to indicate what I mean, for the past ~15 years,
the ideal in C++ coding has been to write 'self-
documenting code'. This is source code which reads
more or less like text, as opposed to UNIX and DOS
type comands along the lines of 'ls', 'cd', 'rm'
and so on, which are incomprehensable to a user who
haven't learned them.

So if I were to write a class for a tool to design
IIR filters, I'd structure it along such lines (quasi
C++ code):

class FilterDesignerClass {
// Methods
   void normalizeFilterSpecification(FilterSpecificationClass);
   void designNormalizedAnalogPrototype();
   void BilinearTransformToDigitalDomain();
   void denormalizeDigitalFilter();
   FilterClass = operator(FilterSpecificationClass fs)
   {
       this->normalizeFilterSpecification(fs);
       this->designNormalizedAnalogPrototype();
       this->BilinearTransformToDigitalDomain();
       this->denormalizeDigitalFilter();
       return filter;
   }
};

I would then use this as

FilterDesignerClass MakeFilterFrom;
FilterSpecificationClass Spec;
FilterClass filter;

and all this work provides its ample rewards when
I can write something as simple as

filter = MakeFilterFrom(Spec);

(after having initalized the specification).

The goal, then, is *not* to write as *little* code
as possible, but to write as *understandable* code
(to the human reader, the computer doesn't care) as
possible.

Rune

Subject: Default object output (object oriented programming)

From: per isakson

Date: 1 Dec, 2011 16:37:08

Message: 16 of 18

Rune Allnor <allnor@tele.ntnu.no> wrote in message <0ecfa9cd-47a5-4bfa-846a-6f854353b1a8@h5g2000yqk.googlegroups.com>...

[...]
> > > Rune
> >
> > I believe it indeed is a defined behaviour since Richard Ellis is with The Mathworks.
>> - per
>
> That's not a very convincing way of arguing. Cite the
> specification of matlab's OO sytem, if you have access
> to it. I suspect few if any have, outside TMW. And I
> suspect very few have, even inside TMW.
>
> Apart from that, there are a couple of details that
> make me think this is not at all well-defined:
>
> - There is nothing in the docs to show how to do this
> kind of thing.
> - The trick, if that's what it is, works in some cases
> and not in other, semantically similar ones.
> - The general idea is not at all a good one.
>
> The conclusion is simple: Don't attempt to use that
> kind of functionality. Write well-defined code first
> time around.
>
> Rune

I agree. I don't intend to use this construct if it is not described in the documentation.

Somewhere the doc should describe the situations in which DOUBLE (and CHAR) is called by Matlab. (E.g. as with SUBSREF.)

This thread gave me the opportunity to learn something new - I thought.

I the R2011b documentation on DOUBLE there is this tip
"Tips
The double function should be overloaded for any object when it makes sense to convert it to a double-precision value."

Still confused.

- per

Subject: Default object output (object oriented programming)

From: Rune Allnor

Date: 1 Dec, 2011 16:58:52

Message: 17 of 18

On 1 Des, 17:37, "per isakson" <p...@bim.kth.se> wrote:
> Rune Allnor <all...@tele.ntnu.no> wrote in message <0ecfa9cd-47a5-4bfa-846a-6f854353b...@h5g2000yqk.googlegroups.com>...
>
> [...]
>
>
>
>
>
> > > > Rune
>
> > > I believe it indeed is a defined behaviour since Richard Ellis is with The Mathworks.
> >> - per
>
> > That's not a very convincing way of arguing. Cite the
> > specification of matlab's OO sytem, if you have access
> > to it. I suspect few if any have, outside TMW. And I
> > suspect very few have, even inside TMW.
>
> > Apart from that, there are a couple of details that
> > make me think this is not at all well-defined:
>
> > - There is nothing in the docs to show how to do this
> >   kind of thing.
> > - The trick, if that's what it is, works in some cases
> >   and not in other, semantically similar ones.
> > - The general idea is not at all a good one.
>
> > The conclusion is simple: Don't attempt to use that
> > kind of functionality. Write well-defined code first
> > time around.
>
> > Rune
>
> I agree. I don't intend to use this construct if it is not described in the documentation.
>
> Somewhere the doc should describe the situations in which DOUBLE (and CHAR) is called by Matlab. (E.g. as with SUBSREF.)
>
> This thread gave me the opportunity to learn something new - I thought.
>
> I the R2011b documentation on DOUBLE there is this tip
> "Tips
> The double function should be overloaded for any object when it makes sense to convert it to a double-precision value."
>
> Still confused.

I can't see any situations where that makes sense:

- If it makes sense to view the object as a DOUBLE
  then the object *is* a DOUBLE.
- If the object *contains* a DOUBLE, then use an
  access method to access that member variable.

The only times I have wanted to derive types from
Plain Old Data types (PODs) in C++ is when I wanted
to use the argument type checking properties of
functions o distingush between different types
of integer indexes into arrays.

As I tried to indicate in a different post, the
goal is to write as understandable code as possible.
The code will be read (and debugged) more times
than it will be written, so write code for the
confused reader's benefit, not the busy (or even
lazy...) coder's.

Rune

Subject: Default object output (object oriented programming)

From: per isakson

Date: 1 Dec, 2011 17:38:08

Message: 18 of 18

Rune Allnor <allnor@tele.ntnu.no> wrote in message <0f5fc637-b87a-475e-a36f-
[...]
> > Still confused.
>
> I can't see any situations where that makes sense:
>
> - If it makes sense to view the object as a DOUBLE
> then the object *is* a DOUBLE.
> - If the object *contains* a DOUBLE, then use an
> access method to access that member variable.
>
> The only times I have wanted to derive types from
> Plain Old Data types (PODs) in C++ is when I wanted
> to use the argument type checking properties of
> functions o distingush between different types
> of integer indexes into arrays.
>
> As I tried to indicate in a different post, the
> goal is to write as understandable code as possible.
> The code will be read (and debugged) more times
> than it will be written, so write code for the
> confused reader's benefit, not the busy (or even
> lazy...) coder's.
>
> Rune

I fully agree with your last paragraph. I try my best with Matlab.

The documentation does not convince me there is any good reasons for overloading double.

Thank you for the discussion!

- per

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