Perl in MATLAB
Get MATLAB to reference a second Perl installation.

When I updated a local MATLAB install to 2016a, I ran into the same problem with Math::Random.  In this post, I wrote about how I fixed MATLAB for use of a local Perl install. (I found it, again, when searching for a solution to this problem!)  But, this fix didn’t work again so I set out to uncover a new working solution.


Perl in MATLAB

When running a piece of MATLAB code that called a Perl function, I would receive the following error:

Error using perl (line 79)
System error: Can't locate Math/ in @INC (@INC contains: C:/Program
Files/MATLAB/R2016a/sys/perl/win32/site/lib C:/Program Files/MATLAB/R2016a/sys/perl/win32/lib .) at line 3.
BEGIN failed--compilation aborted at line 3.
Command executed: perl -t t -n 20

MATLAB was referencing an internal Perl installation that did not have the module.  I needed to install that module.  Here’s what I tried:

  1. I located the built-in MATLAB Perl (an ActivePerl install) installation.  I attempted to use it’s built-in CPAN tool and the built-in PPM tool.  Neither were successful.
  2. I went and got the ActivePerl installer for the architecture.  I installed it and then ran the CPAN tool associated with this Perl installation; it worked this time.  From the CPAN terminal installation, I typed this  install Math::Random and installed the missing packages.
  3. I made sure the path was in the correct order via my previous solution–just in case.
  4. In MATLAB’s Perl.m file, I changed the line
    perlCmd = fullfile(matlabroot, 'sys\perl\win32\bin\');
    perlCmd = fullfile('C:\Perl64\bin\');.

Now, MATLAB would look at my ActivePerl local installation.


With a previous solution not working, I set about finding a new one.  I learned the use of the MATLAB command ‘fullfile’ and got a working Perl install to be recognized in 2016a.  I ensured that my new Perl install recognized the proper module that I required.  My code now runs flawlessly.

If you have any questions or comments, please feel free to leave them below.  If you enjoyed what you read, please subscribe to the blog or any of my social media accounts.  Finally, feel free to share this link through social media.

Perl in MATLAB
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *