Main Content

pcode

Create content-obscured, executable files

Description

pcode(item) obfuscates the code in a .m file or folder on the search path and produces P-code files with the extension .p. For example, if item is a .m file named mytest.m, then the resulting file is mytest.p. If item is a folder, then all script or function files in that folder are obfuscated and saved in the current folder. A P-code file takes precedence over the corresponding .m file for execution, even after modifications to the .m file.

Note

Security Considerations: The pcode function produces MATLAB® program files in a proprietary, obfuscated code format. Consider combining multiple approaches to protect sensitive code or data. For more information, see Security Considerations to Protect Your Source Code.

example

pcode(item,"-R2022a") creates the P-code files using a more complex obfuscation algorithm. Files obfuscated in this way run only in MATLAB R2022a and later.

example

pcode(item,"-R2007b") creates the P-code files using the default, legacy algorithm.

pcode(item1,item2,...,itemN) creates P-code files from each .m file or folder specified in a comma-separated list.

example

pcode(___,"-inplace") creates the P-code files in the same folders as the inputs. Specify "-inplace" after any of the input argument combinations in the previous syntaxes.

If the source file resides within a namespace or class folder, then pcode creates the same namespace or class structure to house the resulting P-code files.

example

Examples

collapse all

In a file named myfunc.m in your current folder, define a function that returns the square root of a cubic polynomial.

function y = myfunc(x)
y = sqrt(x.^3 + x.^2 + x + 1);
end

Create a P-code file from myfunc.m.

pcode myfunc

Confirm that MATLAB uses the P-code file when you call myfunc.

a = myfunc(3);
which myfunc
c:\myMATLABfiles\myfunc.p

In a file named myfunc.m in your current folder, define a function that returns the square root of a cubic polynomial.

function y = myfunc(x)
y = sqrt(x.^3 + x.^2 + x + 1);
end

Create a P-code file from myfunc.m using a more complex obfuscation algorithm. Files obfuscated in this way run only in MATLAB R2022a and later.

pcode myfunc -R2022a

Confirm that MATLAB uses the P-code file when you call myfunc.

a = myfunc(3);
which myfunc
c:\myMATLABfiles\myfunc.p

Convert selected files from the sparfun folder to P-code files.

Create a temporary folder and define an existing path to .m files.

tmp = tempname;
mkdir(tmp)
cd(tmp)
filename = fullfile(matlabroot,"toolbox","matlab","sparfun","spr*.m");

Create the P-code files.

pcode(filename)
dir(tmp)
.            ..           sprand.p     sprandn.p    sprandsym.p  sprank.p     

The temporary folder now contains encoded P-code files.

Generate P-code files from input files that are part of a class. (You can apply the same procedure to files that are part of a namespace.)

Define classfolder as an existing class folder that contains .m files.

classfolder = fullfile(cd,"@myClass")
classfolder = 
"/tmp/Bdoc24b_2679053_73809/tp0dd762d3/matlab-ex48377006/@myClass"
dir(classfolder)
.          ..         myClass.m  

Create a temporary folder. This folder has no class structure at this time.

tmp = tempname;
mkdir(tmp)
cd(tmp)
dir(tmp)
.   ..  

Create a P-code file for every .m file in the path classfolder. Because the input files are part of a class, MATLAB creates a folder structure so that the output file belongs to the same class.

pcode(classfolder)
dir(tmp)
.         ..        @myClass  

The P-code file resides in the same folder structure.

dir("@myClass")  
.          ..         myClass.p  

Generate P-code files in the same folder as the input files.

Copy several .m files to a temporary folder.

filename = fullfile(matlabroot,"toolbox","matlab","sparfun","spr*.m");
tmp = tempname;
mkdir(tmp)
copyfile(filename,tmp)
dir(tmp) 
.            ..           sprand.m     sprandn.m    sprandsym.m  sprank.m     

Create P-code files in the same folder as the original .m files.

pcode(tmp,"-inplace")
dir(tmp)
.            ..           sprand.m     sprand.p     sprandn.m    sprandn.p    sprandsym.m  sprandsym.p  sprank.m     sprank.p     

Input Arguments

collapse all

.m file or folder to obfuscate, specified as a character vector or string scalar.

  • An input argument that does not have a file extension and is not the name of a folder must be a function on the MATLAB path or in the current folder.

  • When using the wildcard character *, pcode ignores all files without a .m extension.

  • The pcode function does not support live scripts or functions (.mlx).

  • If item resides within a namespace or class folder, then pcode creates the same namespace or class structure to house the resulting P-code files.

List of .m files or folders to obfuscate, specified as a comma-separated list of character vectors or string scalars. The list can include a mix of files and folders.

More About

collapse all

Creating P-Code Files from Related Files

In addition to your program, you can obfuscate other functions and scripts that your program is dependent upon. To determine the files required to run your program, use the matlab.codetools.requiredFilesAndProducts function.

Obfuscating Code

P-code files are an obfuscated, execute-only form of MATLAB code. You cannot open a P-code file in the MATLAB Editor or Live Editor.

Version History

Introduced before R2006a

expand all