Avoid code generation string.h or memcpy for typecast()
Ältere Kommentare anzeigen
Hi,
I want to generate C code with Matlab Coder.
I already set in the Advanced Setting: Use memcpy for vector assignment: No
and
Use memset to initialize floats and doubles to 0.0 : No
But for Matlab Code:
typecast(x,'uint32')
or
typecast(x,'single')
it is still using: (void) memcpy
Are there more settings to avoid using string.h and memcpy in specific? Thanks.
Antworten (1)
Shivam Malviya
am 11 Nov. 2022
Bearbeitet: Shivam Malviya
am 11 Nov. 2022
Hi Roman,
It is my understanding that you are generating C code from MATLAB code that contains a “typecast” function.
The generated C code uses “string.h” and “memcpy” function, which you want to avoid.
Also, setting the options to the following value did not help;
- “Use memcpy for vector assignment”: No
- “Use memset to initialize floats and doubles to 0.0”: No
As a workaround, the Code Replacement Library (CRL) can be used.
Please follow the workflow mentioned below to achieve the same.
Creating Code Replacement Library for "memcpy"
- Download the attached scripts "crl_table_memcpy.m" and "rtwTargetInfo.m" and save them in the current working directory or in the folder that is in MATLAB Path.
- To use your code replacement library, refresh your current MATLAB session with the command:
>> sl_refresh_customizations
3. Verify the code replacement library by executing the command
>> crviewer('CRL for memory function replacement')
Code generation
- In Generate Code stage of code generation using MATLAB Coder App, open more settings, and set the options' value as below;
a. “Code replacements”: Yes
b. “Code replacement library”: CRL for memory function replacement
2. Generate the code
After using Code Replacement Library, it can be observe that “memcpy” is replaced with “my_memcpy”;
- Before

- After

Below are the links to detailed documentation of the steps mentioned above;
- Memory Function Code Replacement - MATLAB & Simulink
- Identify Code Replacement Library Requirements - MATLAB & Simulink
Hope this helps!
Regards,
Shivam Malviya
Kategorien
Mehr zu MATLAB Coder finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!