Main Content


Package: coder

Control code generation from P-code files



coder.allowpcode('plain') allows you to generate P-code files that you can then compile into optimized MEX functions or embeddable C/C++ code. This function does not obfuscate the generated MEX functions or embeddable C/C++ code.

With this capability, you can distribute algorithms as P-code files that provide code generation optimizations.

Call this function in the top-level function before control-flow statements, such as if, while, switch, and function calls.

MATLAB® functions can call P-code. When the .m and .p versions of a file exist in the same folder, the P-code file takes precedence.

coder.allowpcode is ignored outside of code generation.


collapse all

Write a function p_abs that returns the absolute value of its input:

function out = p_abs(in)   %#codegen
% The directive %#codegen indicates that the function
% is intended for code generation
out = abs(in);

Generate P-code file. In the MATLAB Command Window, enter:

pcode p_abs
The P-code file, p_abs.p, appears in the current folder.

Generate a MEX function for p_abs.p, using the -args option to specify the size, class, and complexity of the input parameter (requires a MATLAB Coder™ license).

codegen p_abs -args { int32(0) }
codegen generates a MEX function in the current folder.

If you have MATLAB Coder, generate embeddable C code for p_abs.p.

codegen p_abs -config:lib -args { int32(0) };
codegen generates C library code in the codegen\lib\p_abs folder.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced in R2011a

See Also

| (MATLAB Coder)