matlab pcode (m文件保护文件)

本文详细介绍了如何使用MATLAB的pcode函数将.m文件转换为P文件,以实现代码保护。包括单个文件和多个文件的转换方法,以及如何在原文件所在目录下创建P文件。注意P文件的生成方式可能影响其兼容性,且P文件并不提供足够的安全性来保护知识产权。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://cn.mathworks.com/help/matlab/ref/pcode.html

Create protected function file

Syntax

pcode(fun)
pcode(fun1,...,funN)
pcode(fun,'-inplace')

Description

pcode(fun) obfuscates the code in fun.m and produces a file called fun.p, known as a P-file. If fun is a folder, then all the script or function files in that folder are obfuscated in P-files. MATLAB® creates the P-files in the current folder. The original .m file or folder can be anywhere on the search path.

pcode(fun1,...,funN) creates N P-files from the listed files. If any inputs are folders, then MATLAB creates a P-file for every .m file the folders contain.

pcode(fun,'-inplace') creates P-files in the same folder as the script or function files.

  • Note:   The pcode function obfuscates your .m files, it does not encrypt them. While the content in a .p file is difficult to understand, it should not be considered secure. It is not recommended that you P-code files to protect your intellectual property.

Input Arguments

fun

MATLAB file or directory containing MATLAB files. If fun resides within a package and/or class folder, then pcode creates the same package and/or class structure to house the resulting P-files.

An input argument with no file extension and that is not a folder must be a function in the MATLAB path or in the current folder.

When using wild cards *pcode ignores all files with extensions other than .m.

Examples

collapse all

P-Coding Multiple Files

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)
fun = fullfile(matlabroot,'toolbox','matlab','sparfun','spr*.m');

Create the P-files.

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

The temporary folder now contains encoded P-files.

P-Coding Files That Belong to a Package and/or Class

Generate P-files from input files that are part of a package and/or class. This example uses an existing MATLAB example class.

Define funclass as an existing a class folder that contains .m files.

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

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


.              ..             BankAccount.m  

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

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

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

pcode(funclass)
dir(tmp)
.             ..            @BankAccount 

You see that the P-file resides in the same folder structure.

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

P-Coding In Place

Generate P-files in the same folder as the input files using the option inplace

Copy several MATLAB files to a temporary folder.

fun = fullfile(matlabroot,'toolbox','matlab','sparfun','spr*.m');
tmp = tempname;
mkdir(tmp);
copyfile(fun,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  

More About

expand all

Tips

  • The pcode algorithm was redesigned in MATLAB 7.5 (Release R2007b). If your P-file was generated prior to MATLAB 7.5, it will not run in MATLAB 8.6 (Release R2015b) or later. Files generated in 7.5, or later versions, cannot run in MATLAB 7.4 or earlier.

  • When obfuscating all files in a folder, pcode does not obfuscate any files within subfolders.

  • A P-file takes precedence over the corresponding .m file for execution, even after modifications to the .m file.

  • MATLAB does not display any of the help comments that might be in the original .m file.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值