Directly talking to a computer is a difficult thing to do, especially when your computer can execute billions of operations each second. We need an intermediate that convert our language into basic elements of computations. In the early days, this was done using punched cards. We now rely on computer programs called “Compilers” that convert our words into bits and operations on bits.
I could go on forever on the history of programming (maybe I will some day) but my point today is to talk about an important side effect of the need for compilers, encryption. Because the code needs to be compiled and because this compilation process can be very long, program are not usually shared in their readable form but in their compiled form, in machine language. Since we don’t read this language, it basically looks encrypted.
In the early days, it looked natural to only share this way but the computer community quickly realized that this could cause many problems as computer complexity and all the associated software was increasing. This is how the Open Source movement started.
Matlab is a little bit of this and a little bit of that. Many core functionalities of Matlab are ‘encrypted’ or built-in. Still lots of functions are Open-source, you can open its associated M-file. Mathworks have tried to create a sense of open community and many Matlab users share M-file through the Mathworks file-exchange. This is a great and very efficient tool, especially to us researchers. If you have never looked into it, stop now this readings and check it out.
But, in many case, you actually want to protect your work. You are happy to share the program, but you spent so much time on the code that it could be difficult to give away years of development. This is totally understandable.
If you are in that position, you have more or less 3 options.
- You convert your code to C/C++ or any other language that you compile
- You actually compile your code using Matlab compiler
- You don’t compile but you encrypt your source.
Option 1 and 2 are most of the time known, but most people don’t know that they actually can bypass the compilation part and just encrypt their source. Compiling can be quite annoying if you want to cover all the 6 big plateforms (Windows 32 bit, Windows 64 bit, Mac OS 32 bit, Mac OS 64 bit, Linux 32 bit, Linux 64 bit). Maybe the compiling toolbox is not available to you also. Anyway I will certainly make a post about these processes one day as compiling your code can be a good last alternative to speed up some calculations.
Let’s talk about option 3 then. A big advantage to this is that you don’t have to release various versions of your compiled program for each plateform.
This is actually surprisingly easy.
Let’s create this function TestPcode.m
function y=TestPcode(x) % This function test pcode y=x.^2+10;
Calling TestPcode(3) send you back 14. Now to generate an encrypted version, a P-file, you just have to do :
Matlab will generate the P-file in the same location where the M-file is. If you delete the M-file, TestPcode still works. But the P-file is encrypted, you can’t open it to look at the code.
There are a couple important things. If you want the call to “help TestPcode” to work, you must provide a truncated M-file with just :
function y=TestPcode(x) % This function test pcode
This is how Mathworks actually distribute its built-in functions (ie with a associated M-file to provide the help).
Another important thing is that this also work with function generated with the GUIDE. The GUIDE is used to make interface for your users needs. GUIDE generates a .fig file and .m file. Apply pcode to the .m file and truncate the .m file to only leave the help.