Code generation changes behavior from Matlab 2020b to 2023b

조회 수: 12 (최근 30일)
Steffen
Steffen 2024년 3월 28일
댓글: Satwik 2024년 9월 20일
"Not too long ago, I switched from Matlab version 2020b to 2023b, upgrading all Simulink files in the process. Now, I'm encountering an issue where the Simulink code generation structures the C files differently (see the code snippet below), causing the compiler to fail to compile them.
C-Code from Matlab-Version 2020b:
actuator_state = (uint8_T)!Process_U.alive;
if (Process_U.actuator_flag) {
actuator_state = (uint8_T)(int32_T)((int32_T)actuator_state | 2);
}
if (Process_U.state == 2) {
actuator_state = (uint8_T)(int32_T)((int32_T)actuator_state | 4);
}
if (Process_U.state == 3) {
actuator_state = (uint8_T)(int32_T)((int32_T)actuator_state | 8);
}
C-Code from Matlab-Version 2023b
if (Process_U.actuator_flag) {
b_a = (uint8_T)(int32_T)(!Process_U.alive | 2);
} else {
b_a = (uint8_T)!Process_U.alive;
}
if (Process_U.state == 2) {
b_a = (uint8_T)(int32_T)((int32_T)b_a | 4);
}
if (Process_U.state == 3) {
b_a = (uint8_T)(int32_T)((int32_T)b_a | 8);
}
I wanted to ask if you know which setting might be causing this change.
Thank you very much.
  댓글 수: 3
Steffen
Steffen 2024년 4월 2일
Thank you for the response. I will make these adjustments. Yes, we use Embedded Coder, Simulink Coder, and MATLAB Coder. The toolboxes operate in a pipeline that generates this C code.
During compilation, there is this error:
Process.c: In function 'Process_step':
Process.c:2896:30: error: suggest parentheses around operand of '!' or change '|' to '||' or '!' to '~' [-Werror=parentheses]
2896 | b_a = (uint8_T)(int32_T)(!Process_U.alive | 2);
|
Since compilation worked in the last MATLAB version, I thought that the generated C code in MATLAB 2023b might not be suitable for the compiler. Especially since we haven't modified the compiler.
Satwik
Satwik 2024년 9월 20일
Hi Steffan,
The error message you are encountering suggests that the compiler is having trouble with the expression ‘!Process_U.alive | 2’. The issue arises from the mixing of logical (!) and bitwise (|) operators without clear separation, which can lead to ambiguous behavior.
1. If ‘Process_U.alive’ is a boolean or should be used in a logical context, consider using logical operators:
b_a = (uint8_T)(int32_T)(!Process_U.alive || 2);
2. If you intend to perform a bitwise operation, ensure the logic is correct and use the bitwise NOT operator (~):
b_a = (uint8_T)(int32_T)((~Process_U.alive) | 2);

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by