Main Content


Use Code Generation Verification (CGV) API to execute model


cgvObject = slvnvruncgvtest(model, dataFile) invokes the Code Generation Verification (CGV) API methods and executes the model by using all test cases in dataFile. cgvObject is a cgv.CGV object that slvnvruncgvtest creates during the execution of the model. slvnvruncgvtest sets the execution mode for cgvObject to'sim' by default.


cgvObject = slvnvruncgvtest(model, dataFile, runOpts) invokes CGV API methods and executes the model by using test cases in dataFile. runOpts defines the options for executing the test cases. The settings in runOpts determine the configuration of cgvObject.


collapse all

This example shows how to log signals, run tests, and compare results for generated code using the code generation API.

Open the directory that contains the example files.


Open the sldemo_mdlref_basic example model and log the input signals to the CounterA model block.

loggedData = slvnvlogsignals('sldemo_mdlref_basic/CounterA');

Create the default configuration object for slvnvruncgvtest and allow the model to be configured to execute test cases with the CGV API.

runOpts = slvnvruntestopts('cgv');
runOpts.allowCopyModel = true;

Execute the test using the logged signals first in normal mode, and then in software-in-the-loop (SIL) mode.

cgvObjectSim = slvnvruncgvtest('sldemo_mdlref_counter', loggedData, runOpts);
runOpts.cgvConn = 'sil';
cgvObjectSil = slvnvruncgvtest('sldemo_mdlref_counter', loggedData, runOpts);

Compare the results of the first test case.

simout = cgvObjectSim.getOutputData(1);
silout = cgvObjectSil.getOutputData(1);
[matchNames, ~, mismatchNames, ~ ] =, silout);
fprintf('\nTest Case:  %d Signals match, %d Signals mismatch', ...
        length(matchNames), length(mismatchNames));

Input Arguments

collapse all

Model name, specified as a character array or string.

Input data file name, specified as a structure, a character array, or a string. Generate data by either:

  • Using the Simulink® Design Verifier™ software to analyze the model.

  • Using the slvnvlogsignals function.

Simulation run options, specified as a structure array. The fields of runOpts specify the configuration of slvnvruncgvtest.

Field NameDescription

Test case index array to simulate from dataFile.

If testIdx = [] (the default), slvnvruncgvtest simulates all test cases.


If you have not configured your model to execute test cases with the CGV API, this field specifies creating and configuring the model.

If true and you have not configured your model to execute test cases with the CGV API, slvnvruncgvtest copies the model, fixes the configuration, and executes the test cases on the copied model.

If false (the default), an error occurs if the tests cannot execute with the CGV API.


If you have not configured the top-level model or any referenced models to execute test cases, slvnvruncgvtest does not copy the model, even if allowCopyModel is true. An error occurs.


Defines the software-in-the-loop (SIL) or processor-in-the-loop (PIL) approach for CGV:

  • 'topmodel' (default)

  • 'modelblock'


Specifies mode of execution for CGV:

  • 'sim' (default)

  • 'sil'

  • 'pil'


runOpts = slvnvruntestopts('cgv') returns a runOpts structure with the default values for each field.

Output Arguments

collapse all

Code generation verification analysis results, returned as a cgv.CGV (Embedded Coder) object.

slvnvruncgvtest saves the following data for each test case executed in an array of Simulink.SimulationOutput objects inside cgvObject.




Simulation time


State data


Output signal data


Signal logging data for:

  • Signals connected to outports

  • Signals that are configured for logging data on the model


To run slvnvruncgvtest, you must have an Embedded Coder® license.

If your model has parameters that are not configured for executing test cases with the CGV API, slvnvruncgvtest reports warnings about the invalid parameters. If you see these warnings, do one of the following:

  • Modify the invalid parameters and rerun slvnvruncgvtest.

  • Set allowCopyModel in runOpts to be true and rerun slvnvruncgvtest. slvnvruncgvtest makes a copy of your model configured for executing test cases, and invokes the CGV API.

You can not run slvnvruncgvtest on a model that uses configuration set references. Consider changing your model to contain a copy of the configuration set instead of a reference to it.

Version History

Introduced in R2010b