Although MATLAB® source (.m) code is executable by itself, the contents of MATLAB source files are easily accessed, revealing design and implementation details. If you do not want to distribute your proprietary application code in this format, you can use one of these more secure options instead:
Deploy as P-code — Convert some or all of your source code files to a content-obscured form called a P-code file (from its .p file extension), and distribute your application code in this format.
Compile into binary format — Compile your source code files using the MATLAB Compiler to produce a standalone application. Distribute the latter to end users of your application.
In general, if you want to run the code as a standalone application outside of MATLAB, it is best to use the MATLAB Compiler™ to make your code secure . If you plan to run the code within the MATLAB environment, there is no need to run the Compiler. Instead, convert to P-code those modules of your source code that need to be secure.
A P-code file behaves the same as the MATLAB source from which it was produced. The P-code file also runs at the same speed as the source file. Because the contents of P-code files are purposely obscured, they offer a secure means of distribution outside of your organization.
Note: Because users of P-code files cannot view the MATLAB code, consider providing diagnostics to enable a user to proceed in the event of an error.
To generate a P-code file, enter the following command in the MATLAB Command Window:
pcode file1 file2, ...
The command produces the files, file1.p, file2.p, and so on. To convert all .m source files residing in your current folder to P-code files, use the command:
See the pcode function reference page for a description of all syntaxes for generating P-code files.
You invoke the resulting P-code file in the same way you invoke the MATLAB .m source file from which it was derived. For example, to invoke file myfun.p, type
[out, out2, ...] = myfun(in1, in2, ...);
To invoke script myscript.p, type
When you call a P-code file, MATLAB gives it execution precedence over its corresponding .m source file. This is true even if you happen to change the source code at some point after generating the P-code file. Remember to remove the .m source file before distributing your code.
P-Code files are designed to be independent of the release under which they were created and the release in which they are used (backward and forward compatibility). New and deprecated MATLAB features can be a problem, but it is the same problem that would exist if you used the original MATLAB input file. To fix errors of this kind in a P-code file, fix the corresponding MATLAB input file and create a new P-code file.
P-code files built using MATLAB Version 7.4 and earlier have a different format than those built with more recent versions of MATLAB. You still can use these older P-code files when you run MATLAB 7.4 and later, but this capability could be removed in a future release. MathWorks® recommends that you rebuild any P-code files that were built with MATLAB 7.4 or earlier using a more recent version of MATLAB, and then redistribute them as necessary.
Another way to protect your source code is to build it into a standalone executable and distribute the executable, along with any other necessary files, to external customers. You must have the MATLAB Compiler and a supported C or C++ compiler installed to prepare files for deployment. The end user, however, does not need MATLAB.
To build a standalone application for your MATLAB application, develop and debug your application following the usual procedure for MATLAB program files. Then, generate the executable file or files following the instructions in Steps by the Developer to Deploy to End Users in the MATLAB Compiler documentation.