If-Elseif-Else Code to Switch-Case Statements for truth table

Hi, Following Enhance Code Readability for MATLAB Function Blocks and Enhance Readability of Code for Flow Charts I tried to generate the code of a truth table with Embedded Coder. However, I always get an If/ElseIf/Else statement. I tried to simplify my table (only 2 conditions), and then directly to use the chart generated by the truth table by integrating it in a Simulink Chart sub-function. I never get a switch statement.
How to do it right ? Thanks.

4 件のコメント

Rik
Rik 2017 年 11 月 28 日
Not knowing your context, an if-elseif-else block is equivalent to switch-case-case-otherwise.
Jan
Jan 2017 年 11 月 28 日
How to do this right?
How do you define "right"? What are you doing exactly? What are the available inputs and what is the wanted output?
AN.
AN. 2017 年 11 月 29 日
I try to convert (1):
if (in == 1) {
out = 1;
} else if (in == 2) {
out = 2;
} else {
out = 0;
}
to (2):
switch(in) {
case 1:
out = 1;
break;
case 2:
out = 2;
break;
default:
out = 0;
}
With chart proposed above I get solution (1) only. I agree (1) and (2) are equivalent, but readability is improved in more complex cases.
Rik
Rik 2017 年 11 月 29 日
You are mixing Matlab syntax with something that looks like either Java or pseudocode. You don't need a break in a switch (and it might even break a for-loop this might be nested in). Remove all curly brackets and add a closing end to make it valid Matlab syntax.
Also, like Jan, I don't understand what it is you want to do. So if this doesn't help, you'll need to explain more about your goals, possible inputs and desired outputs.

サインインしてコメントする。

回答 (0 件)

質問済み:

AN.
2017 年 11 月 28 日

コメント済み:

Rik
2017 年 11 月 29 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by