Perl in MATLAB for Windows

I now need to run a piece of code for many days, with many variants, on a hefty server (running Windows).  The main piece of code is written for MATLAB and calls a piece of Perl code.

I downloaded and installed the correct version (64-bit) Strawberry Perl. I followed the download instructions, added the correct environment variables, and completed the installation. However, MATLAB was using a crippled version of Perl.

After installing Perl, I needed the package random.pm. So, I issued the command

c:\cpan Math::Random

and Strawberry Perl went and downloaded the module from CPAN. I could run the code that I needed from the command prompt, but not from inside MATLAB.

I used a solution from this site. Unfortunately, the perl.exe wasn’t where it was supposed to be. The true directory is

c:\Program Files\MATLAB\R2010a\sys\perl\win32\bin>.

Here, I simply (as per the directions) renamed perl.exe to perl-matlab.exe. Now, my code runs excellently using the MATLAB command

>>perl('script.pl','arg1','input1','arg2','input2',...)

I hope this helps someone else out.

Good luck!

rsl

Perl Output Conversion to Number

In the last post, I wrote that Perl, called from MATLAB, returned a character array (i.e., ischar(var)=1). I found, on http://www.mathworks.cn/matlabcentral/newsreader/view_thread/17453, the use of the command sscanf.

SSCANF(S,FORMAT,SIZE) reads data from
MATLAB string variable S, converts it according to the specified
FORMAT string, and returns it in matrix A.

Since I need the output to not be a one-dimensional array, I had to use the “reshape” command as well. Look them both up.

A Bisection Method

The Bisection Method is perhaps the first of the root-finding methods because it works like our minds do. If we know a root exists in an interval [a,b], all we have to do is keep cutting the interval in half and look in the more appropriate half. How do we do that?

First, note that the bisection method relies upon functions

f:R->R.

This function must be continuous on [a,b]. Further, almost all bisection codes rely on the fact that f(a)*f(b)0 at x=a and x=b. We couldn’t verify the existence of roots. Functions such as g(x)=sin(x)+3 have no roots on any interval! Consequently, without further automation, we must check for f(a)*f(b)<0.The bisection method approximates the root atp_i=(a+b)/2.If f(a)*f(p_i)<0, then we pick b=p_i. Otherwise a=p_i. We continually shrink the interval until abs(b-a)< eps.

A version of the bisection method written for MATLAB or Octave is posted at bisect.m. This code requires a function given by handle and an interval [a,b]. Additional options include maximum iterations, tolerance on the root and tolerance on the function value.

I hope this is useful for coursework in basic numerical analysis classes or for basic root finding.

MATLAB Tutorials

At LaGrange College, where I’m an assistant professor in the mathematics department, I was privileged to teach a course on MATLAB. Today was the last day. I’ll start posting hyperlinks to the presentations and any useful .m-files or .mat-files. Hopefully they’ll all help!

The first presentation is posted at:
MATLAB Lecture 1. The following topics are discussed in this lecture:

  • The Origins and Purpose of MATLAB
  • The Pros and Cons of MATLAB
  • Basic discussion of the main desktop (brief) and more detailed discussion of the help and documentation
  • Variable typing and precision (both are basic)
  • Three in-class groupwork exercises

As more of this content gets posted, I hope an internet community finds these useful.

MATLAB 2008b, Mex, and GCC 4.3.0

I run Fedora 9 on my Dell D630 Latitude. This is an excellent machine/software combo. However, I pulled out some older code from my graduate career to run in MATLAB for research purposes and could not get the software to compile. MATLAB would run (I was running 2008a at the time), but would not mex c-code especially built to be ran through a Mex interface. I installed the new version of MATLAB (version 2008b) thinking this might fix some issues. This did not. The same compilation error still remained.

Upon issuing the mex command, I received the warning

Warning: You are using gcc version “4.3.0”. The earliest gcc version supported with mex is “4.0.0”. The latest version tested for use with mex is “4.2.0”. To download a different version of gcc, visit http://gcc.gnu.org

This really didn’t concern me since the gcc compilers are always backward compatible. The next error message was

usr/bin/ld: cannot find -lstdc++
collect2: ld returned 1 exit status

mex: link of ‘ “xxxxxxx.mexa64″‘ failed.

??? Error using ==> mex at 213
Unable to complete successfully.

After googling this problem, I saw that mex wasn’t finding the C++-compiler.

In the past, I’ve had similar errors and I always created a link to libstdc++.so.6 in /usr/lib to whatever the current version was (libstdc++.so.6.0.x typically). However, this file existed but libstdc++.so did not.

I created a symbolic link (do a “man ln” at the terminal) /usr/lib/libstdc++.so to the /usr/lib/libstdc++.so.6.0.10 in the same directory. However, this too did not work, but gave the error

/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.3.0/../../../libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/libstdc++.so when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
collect2: ld returned 1 exit status

Then I noticed in the /usr/local/matlab/sys/os/glnxa64 directory that there was a libstdc++.so.6 and a libstdc++.so.6.0.8. When I made the symbolic link /usr/lib/libstdc++.so to /usr/local/matlab/sys/os/glnxa64/libstdc++.so.6.0.8, the compilation works.

The Wonders of SSH

I’m writing today about the wonders of ssh. This native-to-UNIX tool is the replacement for rsh. SSH (secure shell) is a terminal-based tool which connects to a machine running a corresponding server. The user can then execute commands as if he/she were there.

SSH

This tool is ridiculously handy. I know that admins live by it. Killing jobs, starting servers, executing scripts and jobs, or even rebooting the machine remotely can be done remotely using this tool. If you have admin (root) access, you can do anyything via ssh.

Perhaps my favorite option is the ‘-X’ flag. This stands for X-Forwarding. In other words, with this enabled, if I launch programs remotely, I get the program popping up on my own desktop. How fabulous! For those of us who live in MATLAB a remote connection to a faster machine with tons of RAM is priceless.