View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
deploypcode(source_dir, target_dir, varargin)

4.9 | 9 ratings Rate this file 11 Downloads (last 30 days) File Size: 11.4 KB File ID: #26919 Version: 1.10
image thumbnail

deploypcode(source_​dir, target_dir, varargin)


Sven (view profile)


11 Mar 2010 (Updated )

DEPLOYPCODE recursively searches a directory for .m files, creating encrypted .p files with .m help

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

This entry is ideal for those who have a project they need to deploy to P-files, but they want to retain HELP contents for all of their deployed files.
DEPLOYPCODE recursively creates p-code versions of m-files in a given folder
DEPLOYPCODE(SOURCE, TARGET) will recursively search the SOURCE folder for all .m files and
deploy them as P-files to the TARGET folder using default options below.

DEPLOYPCODE(..., 'PropertyName',VALUE,'PropertyName',VALUE,...) uses the following options
when deploying p-code:

  'recurse' - When true (default) will recursively deploy p-code from SOURCE folder.

  'ignoreStrings' - String or cell array of strings of filenames to ignore. Regular expression
                    syntax is used.

  'ignoreSVN' - Additional true/false option to ignore ".svn" directories. Equivalent to
                    adding {'^\.svn$'} to "ignoreStrings". Defaults to TRUE for convenience.

  'includeHelp' - MATLAB's pcode() removes all comments including the help section from
                    files when run. Setting this option to true (default) will export an
                    additional .m file containing only the help contents of the file being
                    deployed. This means that myFunction.m will be deployed to myFunction.p,
                    but calling "help myFunction" will still return help text.

  'updateOnly' - When true, destination p-files will only be created if they don't yet
                    exist or are older than the m-file being encoded. Defaults to false.

  'copyDirectStrings' - String or cell array of strings of filenames to copy directly to TARGET
                    folder without encryption. Regular expression syntax is used.

  'copyFigFiles' - Additional true/false option to copy ".fig" files directly. Equivalent to
                    adding {'\.fig$'} to "copyDirectStrings". Defaults TRUE for convenience.
  'copyDllFiles' - Additional true/false option to copy ".dll" files directly. Equivalent to
                    adding {'\.dll$'} to "copyDirectStrings". Defaults TRUE for convenience.
  'copyMexFiles' - Additional true/false option to copy mex files directly. Defaults TRUE
                    for convenience.

  'flattenFileTree' - When true (default), regular m-files are deployed as p-files only to the
                    TARGET folder initially provided (rather than under any subdirectories).
                    When this option is set to FALSE, the file structure under the SOURCE
                    directory will be replicated at the TARGET.


written by Sven Holcombe Oct 2011

Author's note:
While I am generally against unnecessary encryption of .m files (it would be against the spirit of this file exchange, for one), I needed a tool such as this one for my work, and thought that others may need the same.


Genpath Exclude inspired this file.

This file inspired Deploy Pcoded Matlab Files.

MATLAB release MATLAB 7.13 (R2011b)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (14)
11 Oct 2016 Alfredo

This is exactly what I was looking for!!!
Thank you Sven.
I had a problem where developers would go to the production machine and "fine tune" the code after it had been officially released.
Deploying pcode only with your function will eliminate that problem.
Thank you!!

21 Feb 2016 Alexander Brodsky

21 Oct 2015 cao tri

12 Jul 2013 Pete

Pete (view profile)

Excellent. When flattenFileTree == false, it works like a charm. Good work, many thanks!

22 Mar 2013 mayuresh

very good script. Excellent applicability.

03 Sep 2012 Joachim

27 Jun 2012 Andrea Vaccaro

Forgot to rate...

27 Jun 2012 Sven

Sven (view profile)

Thanks Adrea, glad it helped you, and thanks very much for the updates. I'll take a look and incorporate them in the next update I make.

Comment only
27 Jun 2012 Andrea Vaccaro

Wonderful work, saved me a lot of time!!!

I noticed that it would choke on files that aren't syntactically correct. I modified your code to just skip those files and issue a warning. Also I added an option for copying dll files and mex files. I know its a nuance as I could have used the 'copyDirectStrings' options but I found that might be very common to copy those files. Here is the patch corresponding to my modifications:

if you want to include this patch into the code feel free to modify at your wish

cheers and again thanks for your code

Comment only
25 May 2012 Sven

Sven (view profile)

@DBS: Glad it was helpful. I've just uploaded a new version that allows 'copyDirectStrings' as a parameter, where you can set file names (via a regexp) that you want to copy directly from source to target.
Is this what you were looking for?

Comment only
23 May 2012 DBS

DBS (view profile)

Great function Sven, saved me a lot of time! But if it transferred other optional files it would be perfect. For instance in my case, I needed to deploy some .txt, .pdf and .xls from source. Meanwhile, I'll try to insert it in your code. Cheers!

17 Feb 2012 Matthew

Great work. This is very nice code. It is obvious that you enjoyed coding this function. Comments like "Tricky little function..." and "Sneakily force the..." are gems :)

15 Feb 2012 Sven

Sven (view profile)

@Greg: Sure, I've uploaded a new version that allows the flattenTreeFile = false option to take effect.

Comment only
15 Feb 2012 Greg Perrott

Any chance you could implement the option to turn off flattenFileTree?

Great script by the way.

Comment only
20 Oct 2011 1.1

Added .m help-file deployment, and rewrote to remove dependency on genpath_recurse thus improve speed.

11 Nov 2011 1.2

Added "updateOnly" functionality and improved the "p-file is older than m-file" issue generated when a class function file is p-coded.

15 Feb 2012 1.3

Added a "flattenTreeFile = false" option, as requested by Greg

29 May 2012 1.4

Added 'copyDirectStrings' parameter to allow direct copy of chosen files as per DBSs request

20 Aug 2012 1.5

Added two very useful addition to direct copy (.dll and mex), much thanks to Andrea Vaccaro for his suggestions!

21 Aug 2012 1.6

Missed an update from Andrea: wrapping the pcode() call in a try-catch to avoid dying on poorly formatted m-files.

21 Aug 2012 1.7

Sorry - fixed some typos in the description

21 Aug 2013 1.9

Minor change: used an onCleanup() object to return to the correct path. More error-safe.

08 Jan 2015 1.10

Under-the-hood directory parsing function changed in 2014b causing an error. This update makes sure it runs without error on both old and new versions.

Contact us