# Why cases not works in math expression block

## Recommended Posts

Hello, I try to creat a math expression block, with cases function.

I follow excately the example in extend defintion document.

But I don't understand why it not works.

For example, there is a sin waveform generator. Output 1 when signal >0, output -1 when signal <0.

Can anyone takes a look and help.

Thanks.

##### Share on other sites

Hi Yugang,

As I understand it, switch case expressions are not allowed in math expression block (see help). An alternative is the use of OML-block.

Furthermore switch/case has to be used in another way: switch gets an expression, case the result. In your case if/else is the right construct. I've attached an example.

João Marabisa and yugang like this

##### Share on other sites

RoKet is right switch/case is not allowed but...

You can get exactly what you need with MathExpression block in a much easier way. Please look the attached model.

Trick:

- You set your condition between round brackets, if condition is true, it is like having 1 otherwise 0.

You can use & symbol for "and" and | symbol for "or" in the formulation of you condition.

-  You multiply the condition by the associated operation that you want to perform when the condition is true (=1).

- When the condition is false it will return 0 and you will not consider the operation associated to it.

- You sum the other conditions multiplied by the associated operations.

This way of using the MathExpression block is really powerful and flexible, I use it a lot.

João Marabisa and yugang like this

##### Share on other sites

Thanks for the answers from RoKet and Livio Mariano.

I think I make mistake between math expression and matrix expression.

The if and switch works well in matrix expression not math.

And the one from Livio Mariano is very tricky and interesting.

Have a nice day.

##### Share on other sites

Both suggestions are really helpful, thank you guys...

I was having a problem using "If Expressions (Pallete - ActivationOperations)" because my output should be a scalar and not a Matrix. And using that option I would have different output ports, and it does not work for my case.

I've decided to try Livio's suggestion (as a challenge) and it worked. Besides tricky, is pretty elegant.

To illustrate you a case, I have 1 input (Td) and all the other variables are set by the user, and it has to respect the following equations:

I had exactly the result that I was expecting! I've attached the file for those who might be interested.

Cheers!

##### Share on other sites

Hi Yugang,

As you have seen, you have several solution.

Note that for performance reason, it is sometimes better to use the MathExpression or MatrixExpression instead of OmlCustomBlock.

The OmlCustomBlock code is executed in the interpreter, the Mathexpression code is converted into c code so it runs faster.

Thanks

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.