3

I know how to initialise an array of objects:

arrayOfA(3,2) = ClassA();

for i = 1:3
    for j = 1:2
        arrayofA(i,j) = ClassA(...);
    end
end

but when I try this for a property:

classdef ClassB

properties
    arrayOfA;
    ...
end

methods
    %% Constructor
    function b = ClassB(...)
        b.arrayOfA(3,2) = ClassA();  % Error!
        ...
    end
end

I get the exception Conversion to double from ClassA is not possible.

I have read that when a default value for a property is not provided, it is initialised to an empty array of doubles. This explains the exception, but how do I set the default value of arrayOfA such that I can fill it with objects?

I have tried:

properties
    arrayOfA(3,2) = ClassA;

but this gives the exception Unbalanced or unexpected parenthesis or bracket.


Edit: I am using MATLAB R2015b, which turns out to affect the solution - see below.

6
  • 2
    Check out this post on UndocumentedMatlab about restricting the classes for properties, and also this documentation page. Commented Nov 13, 2017 at 19:20
  • What version of MATLAB are you using? Commented Nov 13, 2017 at 20:13
  • @gnovice Both R2017a (64-bit) and R2015b (32-bit), in different circumstances. Commented Nov 13, 2017 at 20:15
  • 1
    @Phydeaux: I think the newer class behavior mentioned above by Dev-iL and below by me were introduced sometime in 2016, but I'm not totally sure. If so, it will only work in your 2017 version. Commented Nov 13, 2017 at 20:20
  • 1
    @Phydeaux: ...However, the answer from Sam below using empty should work in both, if that does what you need. Commented Nov 13, 2017 at 20:28

2 Answers 2

3

In the property definition of arrayOfA, you should be able to set it as:

properties
    arrayOfA = ClassA.empty
end

empty is a built-in method of all non-abstract classes for exactly this purpose. It will initialise the array to an empty array of ClassA, rather than an empty array of doubles.

Sign up to request clarification or add additional context in comments.

Comments

2

Starting in version R2016a, you can restrict the property type like so:

classdef ClassB
properties
  arrayOfA ClassA
  ...
end
methods
  % Constructor
  function b = ClassB(...)
    b.arrayOfA(3, 2) = ClassA();
    ...
  end
end

Note there's no = sign. This will require that values assigned to this property must be of class ClassA, of any size.

Starting in version R2017a, you can also restrict the size as per the documentation here:

classdef ClassB
properties
  arrayOfA(3, 2) ClassA
  ...
end
...

2 Comments

While I consider this the best solution, unfortunately I have confirmed that it does not work for at least one of the versions of MATLAB I am constrained to use. Thanks very much for answering.
Note that this method only specifies the type, and will always initialise your array to have size 0x0. It's possible you may want to initialise to a different empty size, for example, you may know that your array will always have two columns, in which case you'd want to initialise it to 0x2. For this, you need to use the empty method, calling it with ClassA.empty(0,2).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.