How do I keep the help-text when creating .p file?

Viewed 211

I have a m.-file with a function inside using "new>function" and name the function "foo".

>> type foo.m

function [outputArg1,outputArg2] = foo(inputArg1,inputArg2)
%FOO Summary of this function goes here
%   Detailed explanation goes here
outputArg1 = inputArg1;
outputArg2 = inputArg2;
end

>> help foo
 foo Summary of this function goes here
    Detailed explanation goes here

After creating a .p-file, however, the help text is not available anymore.

>> pcode foo
>> delete foo.m
>> help foo
foo is a function.

Can I obfuscate/protect my code, while still having help text available?

1 Answers

By design pcode() only contains parsed MATLAB code. This means that any comments in the code, including the help section, will not be available after obfuscation. The officially promoted way of including a help section after obfuscation is to write a separate .M-file with the same name containing the help section. Thus, save a file foo.m:

%%FOO does bar
%
% Inputs:
%
% More help text
% Blabla

Note that you don't have to actually call foo.p inside your foo.m function:

If both a .M and .P file exist, MATLAB will call the P-file for execution, so the .M file only needs to contain the help comments.

Converting .M files to .P files is just obfuscation not encryption or compilation. This means that the possibility to de-obfuscate exists. See protect your source code by The MathWorks (emphasis mine):

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. When MATLAB P-codes a file, the file is obfuscated not encrypted. 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.


As a side-note, something similar can be seen when calling the source code of built-in functions, e.g. edit sum shows nothing but the help for sum, not even a function keyword on top.

Related