0
votes

I need to implement a if/else in simulink to find out if a input is a scalar value or a matrix. Please see, the diagram below :

schematics

Given:

  • Block(1) - is a input that can be a scalar "1" or a matrix "[[0 15];[5 10]]"

  • Block(2) - must return the signal dimension of the input. Ex: 1 for scalar and >1 for a matrix

The requirements are:

  • Everything must work interpreted or compiled (Simulink coder)
  • The final output of blocks (4) and (5) are scalars
  • I have average understanding of CMexSFunctions. So if I need to implement one to solve the problem it is ok

So far, I have had the following problems:

  • I don't at all if what I am planning to do is feasible
  • I don't know how to implement Block(2) to work on compiled mode
  • Even though there is a if/else, simulink performs a pre-check before running to verify if all signal dimensions are ok. During this check, it gives a error saying ex: that Block(5) has a input of matrix

Any Clues?

1

1 Answers

2
votes

Block(2) is the easiest part which can be implemented using the "Probe" block in Simulink library. Your Input at port 1 must be variable sized signal since you are expecting a scalar or matrix.

I assume you are feeding Input(1) to blocks 4 and 5. At model compile time Simulink does not know which one of these blocks are going to run based on the input size. So Simulink needs to assume both blocks may get scalar or matrix. You need to make blocks 4 and 5 not throw error for both scalar and matrix even though they will be used only for one type at run-time.

If you are not able to do this, for the scalar case a simple work around is to place a Selector before block 5 that selects the first sample always. This will let Simulink know that the input to block 5 is always a scalar.