Use the code analyzer and the code generation readiness tool to detect issues at design time. Before generating code, you must fix these issues.
Check Code with the Code Analyzer
Check Code by Using the Code Generation Readiness Tool
Design Considerations When Writing MATLAB Code for Code Generation
Debugging Strategies