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:
OOP: constructing an array of object for a subclass

Subject: OOP: constructing an array of object for a subclass

From: R Wat

Date: 7 Jun, 2010 21:19:06

Message: 1 of 4

I am currently working on an object oriented program which uses a constructor to build an object array. However, I am having problems when it comes to the inheritance of this constructor. For example, as found on the mathworks documentation for creating object arrays (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brd4btr.html) a class would be defined as follows:

classdef DocArrayExample
   properties
      Value
   end
   methods
      function obj = DocArrayExample(F)
         if nargin ~= 0 % Allow nargin == 0 syntax
            m = size(F,1);
            n = size(F,2);
            obj(m,n) = DocArrayExample; % Preallocate object array
            for i = 1:m
               for j = 1:n
                  obj(i,j).Value = F(i,j);
               end
            end
         end
      end
   end
end

However, if I was to then define a subclass as follows:

classdef subDocArrayExample < DocArrayExample
   methods
        function obj = subDocArrayExample(F)
             obj = obj@DocArrayExample(F);
        end
    end
end

Matlab gets very unhappy. If I don't preallocate the memory as found by the superclass' line "obj(m,n) = DocArrayExample; % Preallocate object array" then I don't have any problems. If possible, I would like to be able to preallocate the memory as I'm dealing with large quantities of data.

Does anyone have any ideas? Thanks!

Subject: OOP: constructing an array of object for a subclass

From: Matt J

Date: 7 Jun, 2010 21:54:04

Message: 2 of 4

"R Wat" <rhino7890@aol.com> wrote in message <hujnoa$ejs$1@fred.mathworks.com>...

> Matlab gets very unhappy.
=======

Could you translate "Matlab gets very unhappy" into actual error messages and input data?

Subject: OOP: constructing an array of object for a subclass

From: R Wat

Date: 8 Jun, 2010 12:32:04

Message: 3 of 4

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
>
> Could you translate "Matlab gets very unhappy" into actual error messages and input >data?

From the above code, the superclass works fine:
>> DocArrayExample(ones(4,3))
ans =

  4x3 DocArrayExample

However, this is the error that I get when I try to use the subclass in the same exact implementation:
subDocArrayExample(ones(4,3))
??? The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting from DocArrayExample to
subDocArrayExample:
The following error occurred converting fro

Error in ==> DocArrayExample>DocArrayExample.DocArrayExample at 10
            obj(m,n) = DocArrayExample; % Preallocate object array

Error in ==> subDocArrayExample>subDocArrayExample.subDocArrayExample at 4
             obj = obj@DocArrayExample(F);

Subject: OOP: constructing an array of object for a subclass

From: Matt J

Date: 9 Jun, 2010 14:12:11

Message: 4 of 4

"R Wat" <rhino7890@aol.com> wrote in message <hujnoa$ejs$1@fred.mathworks.com>...

> methods
> function obj = DocArrayExample(F)
> if nargin ~= 0 % Allow nargin == 0 syntax
> m = size(F,1);
> n = size(F,2);
> obj(m,n) = DocArrayExample; % Preallocate object array
==============

You need to change the pre-allocation

obj(m,n) = feval(class(obj));

It seems that, when the superclass instructor is invoked from the subclass, it is passed an obj of the subclass type. As originally written, your code was therefore trying to stick a superclass instance into an array of the subclass. This signals MATLAB to attempt a conversion from a DocArrayExample to a subDocArrayExample, and it doesn't know how.

====================
> for i = 1:m
> for j = 1:n
> obj(i,j).Value = F(i,j);
> end
> end
> end
> end
> end
> end
>
> However, if I was to then define a subclass as follows:
>
> classdef subDocArrayExample < DocArrayExample
> methods
> function obj = subDocArrayExample(F)
> obj = obj@DocArrayExample(F);
> end
> end
> end
>
=========

With the change above, you know also need to handle the zero argument case in the subclass

          function obj = subDocArrayExample(varargin)
              obj = obj@DocArrayExample(varargin{:});
         end

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