Main Content

pcode

Create content-obscured, executable files

Description

example

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

Note

The pcode function obfuscates your program files and does not encrypt them, thus P-files should not be considered secure. P-coding files to protect your intellectual property is not recommended.

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

example

pcode(___,'-inplace') creates the P-files in the same folders as the inputs. Specify '-inplace' after all other input arguments.

Examples

collapse all

Convert a function file into a P-file.

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-file from myfunc.m. Determine which file MATLAB® uses when you call myfunc.

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

Convert selected files from the sparfun folder into P-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-files.

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

The temporary folder now contains encoded P-files.

Generate P-files from input files that are part of a class. (The same procedure can be applied to files that are part of a package.) This example uses an existing MATLAB example class.

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

classfolder = fullfile(docroot,'techdoc','matlab_oop', ...
    'examples','@BankAccount')
dir(classfolder)
classfolder =

C:\Program Files\MATLAB\R2019a\help\techdoc\matlab_oop\examples\@BankAccount


.              ..             BankAccount.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-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)
.             ..            @BankAccount 

The P-file resides in the same folder structure.

dir('@BankAccount')
.              ..             BankAccount.p  

Generate P-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-files in the same folder as the original .m files.

pcode(tmp,'-inplace')
dir(tmp)
.            sprand.m     sprandn.m    sprandsym.m  sprank.m     
..           sprand.p     sprandn.p    sprandsym.p  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 package or class folder, then pcode creates the same package or class structure to house the resulting P-files.

List of .m files or folders, 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

Release Compatibility

The pcode algorithm was redesigned in MATLAB version R2007b. If a P-file was generated prior to R2007b, then it will not run in versions R2015b or later. Files generated in R2007b or later do not run in R2007a or earlier.

P-Coding Related Files

In addition to your program, you may want to 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-files are an obfuscated, execute-only form of MATLAB code. You cannot open a P-file in the MATLAB Editor or Live Editor.

Introduced before R2006a