Differences in C Code Generation for Switch blocks between Matlab 2021b and Matlab 2024b

7 views (last 30 days)
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.
  1 Comment
dpb
dpb on 7 Jul 2025
Edited: dpb on 7 Jul 2025
"When using datatype conversion from boolean to uint32, Matlab 2021b also produces the ternary conditional operator...."
I also find that baffling; show us a code snippet that does this. I don't have the embedded coder, so can't spearmint here.
I'd also like to see the basic code outline of the switch or if block. Since Matlab uses the 1-byte logical class, I'm wondering if you're writing
flag=logicalCondition(something); % returns MATLAB logical
switch flag
case 0
...
case 1
...
end
that it's the integers interpreted as default double in the case statements causing the cast; does the behavior change if you were to write
switch flag
case false
...
case true
...
end
? That should be comparing like types.
The other alternative I'd suggest trying would be to create the flag variable in the integer form initially, forego the logical type altogether if you're just going to promote it to the 32-bit integer anyway.

Sign in to comment.

Answers (1)

dpb
dpb on 4 Jul 2025
Edited: dpb on 4 Jul 2025
As a proprietary product, The Mathworks can choose any implementation they wish that produces the correct end result; the coding standards imposed by your company notwithstanding. (sorta' obvious).
About your only recourse with MATLAB would be to submit a formal support question and see if there would be any internal sympathy for the issue. Just hazarding a guess, you probably would have better luck getting dispensation internally to accept the product revisions instead unless your organization is a major player.
The immediate workaround would be to make a change in the switch variable to be 1,2 instead of 0,1 by using the value of the logic variable plus 1. Of course, your internal code review team probably won't like this, either...and, there's no guarantee The Mathworks won't decide in the future to change the code generator again for any two-variable switch, not just 0,1.
However, there are other possibilities using the one-based variable, besides...the value can then be used as the index in a lookup table instead, for example, which, depending on the application could avoid the test entirely.
  4 Comments
Martin
Martin on 7 Jul 2025
I'm not sure I understand what you mean by "conversion to one-based", can you elaborate a bit on that? What kind of block would I use for that? Thanks in advance for your support.
Interestingly, searching for the term "one-based" led me to a possible solution / workaround for my problem: If I replace the switch with a multiport switch, with the data port order set to "Zero-based contiguous" and 0 and 1 as inputs, I get an if-else in the code just like I did with the regular switch block in Matlab 2021b.
dpb
dpb on 7 Jul 2025
Edited: dpb on 8 Jul 2025
"...what you mean by "conversion to one-based", ..."
Change the switch variable to be 1,2 instead of 0,1 by using the value of the logic variable plus 1.
Then,
...
flag=someLogicExpression(whatever); % 0, 1
LOOKUPTABLE=[result(flag==false) result(flag==true)]; % store the possible results
...
index=flag+1; % 1, 2 so can be array index--MATLAB is not C
result=LOOKUPTABLE(index); % assign the result w/o needing branching construct
...
instead of
...
flag=someLogicExpression(whatever);
if ~flag
result=result0;
else
result=result1;
end
...
In the most simple case, the results might be constants, even.
In complex ones, the lookup array might contain function handles to be dynamically evaluated.

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!