Why is MATLAB Coder not generating complete code for my function in MATLAB R2023b?

I have a MATLAB function that takes a row or column vector, performs some vector computations, removes the first and last elements of the vector, and then returns the vector among other outputs.
When I generate code for this MATLAB function using a basic MATLAB Coder configuration/generation script, the output code is much smaller than expected and missing much of the logic. I see sections with comments indicating what the original MATLAB source code was but then see no actual C++ translation.
When I use the MATLAB Coder GUI instead of my script, the same outcome happens; however, when it checks for run-time errors, some potential errors are flagged. When I use the script, no runtime errors are flagged, so I'm not sure what to make of this.
For context, this is my code generation script:
%% Create configuration object of 'coder.EmbeddedCodeConfig'.
cfg = coder.config('lib','ecoder',true);
cfg.GenerateExampleMain = "GenerateCodeOnly";
cfg.FilePartitionMethod = 'SingleFile';
cfg.TargetLang = 'C++';
cfg.MATLABSourceComments = true;
currentDir = pwd;
cppCodeDir = fullfile(currentDir,'Cpp-Code');
fnNameForCodegen = 'myFunctionForCodegen';
% Note: myArgs is defined above outside snippet.
codegen('-config', cfg, '-d', cppCodeDir, fnNameForCodegen, '-args', myArgs);
Why is the code not generating completely, and how can I get it to generate completely?

 Accepted Answer

MATLAB Coder may prune code that is determined to be unreachable. There is no way to disable pruning in MATLAB R2023b.
To get MATLAB Coder to generate code for all of the logic present in your script, ensure that all the code is reachable. Some portions of your MATLAB function may be deterministically unreachable. Parts that rely on conditions such as for-loops and if-else blocks are likely candidates.
Be aware that the runtime errors caught by the MATLAB Coder graphical user interface present potential problems with your code that should be addressed. The reason that you do not see runtime errors with your current code generation script is because the configuration script you are using has not enabled them.
To check for runtime errors with a code generation script, see the documentation resource "Generate Standalone C/C++ Code That Detects and Reports Run-Time Errors".

More Answers (0)

Products

Release

R2023b

Community Treasure Hunt

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

Start Hunting!