Differences in C Code Generation for Switch blocks between Matlab 2021b and Matlab 2024b
Hi, recently Matlab 2024b was rolled out in our company, and when I startet a new project based on an old project created with Matlab 2021b, I noticed a strange difference in the generated C code for switch blocks:
I use a switch block to set an uint32 output to 1 of 2 different values based on a boolean input.
In Matlab 2021b, I always get an if-else in the code.
In Matlab 2024b, but only if the 2 values are 1 and 0, I get the ternary conditional operator (also called "question mark operator") instead (for other values than 1 and 0, I still get if-else though). Unfortunately, this operator is not permitted according to our company coding standard.
Is there a way to make the embedded coder also always produce if-else in Matlab 2024b? I have not found any settings and also not found any mention of this change in the release notes.
Background info: When using datatype conversion from boolean to uint32, Matlab 2021b also produces the ternary conditional operator. That’s why I used the switch blocks to avoid this construct. Now this exact workaround does not work anymore due to the different behaviour. So alternatively, if there is an easy way to tell the datatype conversion to use a simple cast (after all, the values can only be 1 or 0) instead of either ternary conditional operator or if-else, that would also solve my problem.Hi, recently Matlab 2024b was rolled out in our company, and when I startet a new project based on an old project created with Matlab 2021b, I noticed a strange difference in the generated C code for switch blocks:
I use a switch block to set an uint32 output to 1 of 2 different values based on a boolean input.
In Matlab 2021b, I always get an if-else in the code.
In Matlab 2024b, but only if the 2 values are 1 and 0, I get the ternary conditional operator (also called "question mark operator") instead (for other values than 1 and 0, I still get if-else though). Unfortunately, this operator is not permitted according to our company coding standard.
Is there a way to make the embedded coder also always produce if-else in Matlab 2024b? I have not found any settings and also not found any mention of this change in the release notes.
Background info: When using datatype conversion from boolean to uint32, Matlab 2021b also produces the ternary conditional operator. That’s why I used the switch blocks to avoid this construct. Now this exact workaround does not work anymore due to the different behaviour. So alternatively, if there is an easy way to tell the datatype conversion to use a simple cast (after all, the values can only be 1 or 0) instead of either ternary conditional operator or if-else, that would also solve my problem. Hi, recently Matlab 2024b was rolled out in our company, and when I startet a new project based on an old project created with Matlab 2021b, I noticed a strange difference in the generated C code for switch blocks:
I use a switch block to set an uint32 output to 1 of 2 different values based on a boolean input.
In Matlab 2021b, I always get an if-else in the code.
In Matlab 2024b, but only if the 2 values are 1 and 0, I get the ternary conditional operator (also called "question mark operator") instead (for other values than 1 and 0, I still get if-else though). Unfortunately, this operator is not permitted according to our company coding standard.
Is there a way to make the embedded coder also always produce if-else in Matlab 2024b? I have not found any settings and also not found any mention of this change in the release notes.
Background info: When using datatype conversion from boolean to uint32, Matlab 2021b also produces the ternary conditional operator. That’s why I used the switch blocks to avoid this construct. Now this exact workaround does not work anymore due to the different behaviour. So alternatively, if there is an easy way to tell the datatype conversion to use a simple cast (after all, the values can only be 1 or 0) instead of either ternary conditional operator or if-else, that would also solve my problem. switch block embedded coder MATLAB Answers — New Questions