0
votes

I created a heterogeneous object array in Matlab derived from the matlab.mixin.Heterogeneous class. My array now contains objects of the superclass Cells and subclasses like Neuron, Astrocyte etc. The methods I want to assign to my subclasses are supposed to simulate their behavior depending on different stimuli. In order to calculate the behavior, I need the objects to perform mathematical operations. But when I define a function that uses operators in the methods of the class, Matlab tells me e.g.:

Undefined operator '-' for input arguments of type 'Cells'.

Then I tried to construct my superclass with multiple inheritances ( Cells < matlab.mixin.Heterogeneous & double ), but then this error appears:

Error using Cells: Class 'Cells' cannot be a subclass of matlab.mixin.Heterogeneous and also inherit a 'subsasgn' method from class 'double'.

which is not surprising, as it says in the description of 'matlab.mixin.Heterogeneous'

You cannot override the following methods in your subclasses:

...

subsasgn

As my only other idea would be to define the methods as functions outside the classes, I wanted to ask if there was any possibility to perform mathematical operations in heterogeneous object arrays.

1
You need to define the operators for your class. minus will define - - Suever

1 Answers

0
votes

You need to define the arithmetic operators as methods of your classes otherwise they will not be able to operate on your custom class. For example, to support subtraction, you need to overload minus for your class

methods
    function self = minus(self, other)
        % Loop through the array and apply the - operator to each 
        for k = 1:numel(self)
            self(k).prop1 = self(k).prop1 - other.prop1;
            self(k).prop2 = self(k).prop2 - other.prop2;
        end
    end
end

Note that the minus method for your hetergeneous array needs to ensure that elements of self of different classes are handled appropriately.

A list of all arithmetic operator methods can be found here