Run Polyspace on C/C++ Code Generated from MATLAB Code
After generating C/C++ code from MATLAB® code, you can independently check the generated code for:
Bugs or defects and coding rule violations by using Polyspace® Bug Finder™.
Run-time errors by using Polyspace Code Prover™.
In R2025a: Polyspace does not integrate with the new MATLAB Coder™ app. Use MATLAB scripts to Polyspace analysis as shown in this topic.
Prerequisites
To follow this example:
You must have an Embedded Coder® license.
You must be familiar with how to use the
codegen
command. Otherwise, see the MATLAB Coder Getting Started.You must integrate your Polyspace and MATLAB installations. See Integrate Polyspace with MATLAB and Simulink.
Example Files
This tutorial uses the MATLAB
Coder example averaging_filter
in
.
Here, polyspaceroot
\polyspace\examples\doc_cxx\matlab_coder
is the Polyspace installation folder, such as, polyspaceroot
C:\Program
Files\Polyspace\R2025a
. The example shows a Code Prover analysis. You can
follow a similar workflow for Bug Finder.
Run Polyspace Analysis Using MATLAB
To run Polyspace analysis, first generate C code from the MATLAB function averaging_filter()
as a static library. In
the MATLAB Command Window,
enter:
% Generate code matlabFileName = fullfile(polyspaceroot, 'polyspace',... 'examples','doc_cxx','matlab_coder','averaging_filter.m'); codegenFolder = fullfile(pwd, 'codegenFolder'); codegen(matlabFileName, '-config:lib', '-c', '-args', ... {zeros(1,100,'double')}, '-d', codegenFolder);
Create a pslinkoptions
object to configure your Polyspace
analysis. In the MATLAB Command Window,
enter:
opts = pslinkoptions('ec');
opts
to select a Code Prover
verification, store results in a temporary folder, and open the Polyspace user
interface when the analysis is
complete:opts.VerificationMode = 'CodeProver'; opts.ResultDir = [tempdir 'results']; opts.OpenProjectManager = 1;
Run a Polyspace analysis using the preceding configuration:
[polyspaceFolder, resultsFolder] = pslinkrun('-codegenfolder', codegenFolder, opts);