Matroab OOP inheritance issue with Matlab (Simulink MATLAB function block)

I defined an abstract base class measurementHandler < handle , which defines an interface for all inherited classes. Two subclasses of this class are: a < measurementHandler and b < measurementHandler .

Now I have a function that should return a handle to an instance of any of these subclasses (depending on the arguments of the function) to the caller. Consider something like this:

 function returnValue = foobar(index) if index == 0 returnValue = a(); else returnValue = b(); end end 

This function is enclosed in the MATLAB Function block in Simulink (2013). When I try to simulate a system, I get the following error:

 Type name mismatch (a ~= b). 

Can anyone suggest a workaround for this that still allows me to use OOP and inheritance while using Simulink?

+4
source share
2 answers

This type of template is possible in the MATLAB function block only if the "if" condition can be evaluated at compile time. Types cannot switch at run time. Can you make the index value constant on the call site?

+4
source

The main reason for using this pattern was to iterate over the measurementHandler array, while all of them can have custom implementations. I was able to do this by coder.unroll loop using the coder.unroll directive. Example for the included MTALAB function block:

 function result = handleAllTheMeasurements(someInputs) %#codegen for index = coder.unroll(1:2) measurementHandler = foobar(index); measurementHandler.handleMeasurement(someInputs); end result = something; end 

Thus, the for loop is processed at compile time, and the return type of the function is defined for each individual call.

+2
source

All Articles