1. Prepare m file simpleproduct.m
function C = Simpleproduct (A, B)
C=a*b;
2. Enter coder in the MATLAB work interface and enter the project name Simpleproduct
3. Add the simpleproduct.m file to the project
4. Define the data type of the input variable A/b
5. Generate a MEX function to test whether the program is correct
6. Generate C code, tick generate code only
7. Under the codegen/lib/simpleproduct/of the current directory, you can see that a heap of. C and. h files are generated
$ ls codegen/lib/simpleproduct/
buildinfo.mat rt_nonfinite.h SIMPLEPRODUCT_REF.RSP
html rtw_proj.tmw simpleproduct_rtw.mk
rtgetinf.c rtwtypes.h simpleproduct_ TERMINATE.C
rtgetinf.h simpleproduct.c simpleproduct_terminate.h
rtgetnan.c simpleproduct.h SimpleProduct_types.h
rtgetnan.h simpleproduct_initialize.c
rt_nonfinite.c simpleProduct_ Initialize.h
8. Create a new C project, copy all the. C and. h files under codegen/lib/simpleproduct/to the project and create a new MAIN.C file with the following contents:
- #include <stdlib.h>
- #include <stdio.h>
- #include "simpleProduct.h"
- #include "simpleproduct_initialize.h"
- #include "simpleproduct_terminate.h"
- int Main ()
- {
- Simpleproduct_initialize ();
- printf ("%lf", Simpleproduct (5,6));
- Simpleproduct_terminate ();
- return 0;
- }
9. Compile the project and view the results of the operation
MATLAB automatically generates C code