Archive for the ‘matlab’ Category
Pop quiz: What does the following line of MATLAB code do?
rand('state',10)
If you said ‘It changes the seed of the random number generator to 10’ you get half a point.
‘Only half a point!?’ I hear you say accusingly ‘but it says so in my book [for example, 1-3], why not a full point?’
You only get a full point if you’d said something like ‘It changes the seed of the random number generator to 10 and it also changes the random number generator from the high quality, default Mersenne Twister generator to a lower quality legacy random number generator.‘
OK, how about this one?
rand('seed',10)
This behaves in a very similar manner– it changes both the seed and the type of the underlying generator. However, the random number generator it switches to this time is an even older one that was introduced as far back as MATLAB version 4. It is not very good at all by modern standards!
A closer look
Open up a fresh copy of a recent version of MATLAB and ask it about the random number generator it’s using
>> RandStream.getGlobalStream
ans =
mt19937ar random stream (current global stream)
Seed: 0
NormalTransform: Ziggurat
mt1993ar refers to a particular variant of the Mersenne Twister algorithm— an industry strength random number generator that’s used in many software packages and simulations. It’s been the default generator in MATLAB since 2007a. Change the seed using the modern (since 2011a), recommended syntax and ask again:
>> rng(10)
>> RandStream.getGlobalStream
ans =
mt19937ar random stream (current global stream)
Seed: 10
NormalTransform: Ziggurat
This is behaving exactly as you’d expect, you ask it to change the seed and it changes the seed…nothing more, nothing less. Now, let’s use the older syntax
>> rand('state',10)
>> RandStream.getGlobalStream
ans =
legacy random stream (current global stream)
RAND algorithm: V5 (Subtract-with-Borrow), RANDN algorithm: V5 (Ziggurat)
The random number generator has completely changed! We are no longer using the Mersenne Twister algorithm, we are now using a ‘subtract with borrow’ [see reference 4 for implementation details] generator which has been shown to have several undesirable issues [5-7].
Let’s do it again but this time using the even older ‘seed’ version:
>> rand('seed',10)
>> RandStream.getGlobalStream
ans =
legacy random stream (current global stream)
RAND algorithm: V4 (Congruential), RANDN algorithm: V5 (Ziggurat)
Now, this random number generator is ancient by computing standards. It also has a relatively tiny period of only 2 billion or so. For details see [4]
Why this matters
Now, all of this is well documented so you may wonder why I am making such a big issue out of it. Here are my reasons
- I often get sent MATLAB code for the purposes of code-review and optimisation. I see the old seeding syntax a LOT and the program’s authors are often blissfully unaware of the consequnces.
- The old syntax looks like all it should do is change the seed. It doesn’t! Before 2007a, however, it did!
- The old syntax is written in dozens of books because it was once the default, correct syntax to use.
- Many users don’t read the relevent section of the MATLAB documentation because they have no idea that there is a potential issue. They read a book or tutorial..it says to use rand(‘state’,10) so they do.
- MATLAB doesn’t use the old generators by default any more because they are not very good [4-7]!
- Using these old generators may adversely affect the quality of your simulation.
The bottom line
Don’t do either of these to change the seed of the default generator to 10:
rand('state',10)
rand('seed',10)
Do this instead:
rng(10)
Only if you completely understand and accept the consequences of the older syntax should you use it.
References
1. ‘MATLAB – A practical introduction to programming and problem solving’, 2009,Stormy Attaway
2. MATLAB Guide (Second Edition), 2005, Desmond Higham and Nicholas Higham
3. Essential MATLAB for Engineers and Scientists (Fourth Edition), 2009, Hahn and Valentine
4. Numerical Computing with MATLAB, 2004, Cleve Moler (available online)
5. Why does the random number generator in MATLAB fail a particular test of randomness? The Mathworks, retreived 26th September 2012
6. A strong nonrandom pattern in Matlab default random number generator, 2006, Petr Savicky, retreived 26th September 2012
7. Learning Random Numbers: A Matlab Anomaly, 2008, Petr Savicky and Marko Robnik-Šikonja, Applied Artificial Intelligence, Vol22 issue 3, pp 254-265
Other posts on random numbers in MATLAB
- Parallel Random Numbers in MATLAB #1 – An introduction to random numbers and seeding in MATLAB
- Probability of overlapping subsequences (How likely is it to get overlapping random sequences using two different seeds for the Mersenne Twister algorithm)
Someone recently contacted me complaining that MATLAB could not do a QR factorisation of a variable precision arithmetic matrix. Double precision matrices work fine of course:
>> A=[2 1 3;-1 0 7; 0 -1 -1];
>> [Q R]=qr(A)
Q =
-0.8944 -0.1826 0.4082
0.4472 -0.3651 0.8165
0 0.9129 0.4082
R =
-2.2361 -0.8944 0.4472
0 -1.0954 -4.0166
0 0 6.5320
Variable precision matrices do not (I’m using MATLAB 2012a and the symbolic toolbox here).
>> a=vpa([2 1 3;-1 0 7; 0 -1 -1]); >> [Q R]=qr(a) Undefined function 'qr' for input arguments of type 'sym'.
It turns out that MATLAB and the symbolic toolbox CAN do variable precision QR factorisation….it’s just hidden a bit. The following very simple function, vpa_qr.m, shows how to get at it
function [ q,r ] = vpa_qr( x ) result = feval(symengine,'linalg::factorQR',x); q=result(1); r=result(2); end
Let’s see how that does
>> a=vpa([2 1 3;-1 0 7; 0 -1 -1]); >> [Q R]=vpa_qr(a);
I’ve suppressed the output because it’s so large but it has definitely worked. Let’s take a look at the first element of Q for example
>> Q(1) ans = 0.89442719099991587856366946749251
Which is correct to the default number of variable precision digits, 32. Of course we could change this to anything we like using the digits function.
- Download vpa_qr.m (needs symbolic toolbox)
The Mathworks sell dozens of toolboxes for MATLAB but they are not the only ones doing it. Many 3rd party vendors also sell MATLAB toolboxes and many of them are of extremely high quality. Here are some of my favourites
- The NAG Toolbox for MATLAB – Over 1500 high quality numerical routines from the Numerical Algorithms Group. Contains local and global optimisation, statistics, finance, wavelets, curve fitting, special functions and much much more. Superb!
- AccelerEyes Jacket – Very fast and comprehensive GPU computing toolbox for MATLAB. I’ve used it a lot.
- Multi-precision toolbox for MATLAB – A colleague at Manchester recently told me about this one as his group uses it a lot in their research. I’ve yet to play with it but it looks great.
Which commercial, 3rd party toolboxes do you use/rate and why?
If you like this list, you may also like my list of high quality, free MATLAB toolboxes
A MATLAB user at Manchester University contacted me recently asking about Black-Scholes option pricing. The MATLAB Financial Toolbox has a range of functions that can calculate Black-Scholes put and call option prices along with several of the sensitivities (or ‘greeks‘) such as blsprice, blsdelta and so on.
The user’s problem is that we don’t have any site-wide licenses for the Financial Toolbox. We do, however, have a full site license for the NAG Toolbox for MATLAB which has a nice set of option pricing routines. Even though they calculate the same things, NAG Toolbox option pricing functions look very different to the Financial Toolbox ones and so I felt that a Rosetta Stone type article might be useful.
For Black-Scholes option pricing, there are three main differences between the two systems:
- The Financial Toolbox has separate functions for calculating the option price and each greek (e.g. blsprice, blsgamma, blsdelta etc) whereas NAG calculates the price and all greeks simultaneously with a single function call.
- Where appropriate, The MATLAB functions calculate Put and Call values with one function call whereas with NAG you need to explicitly specify Call or Put.
- NAG calculates more greeks than MATLAB.
The following code example pretty much says it all. Any variable calculated with the NAG Toolbox is prefixed NAG_ whereas anything calculated with the financial toolbox is prefixed MW_. When I developed this, I was using MATLAB 2012a with NAG Toolbox Mark 22.
%Input parameters for both NAG and MATLAB.
Price=50;
Strike=40;
Rate=0.1;
Time=0.25;
Volatility=0.3;
Yield=0;
%calculate all greeks for a put using NAG
[NAG_Put, NAG_PutDelta, NAG_Gamma, NAG_Vega, NAG_PutTheta, NAG_PutRho, NAG_PutCrho, NAG_PutVanna,...
NAG_PutCharm, NAG_PutSpeed, NAG_PutColour, NAG_PutZomma,NAG_PutVomma, ifail] =...
s30ab('p', Strike, Price, Time, Volatility, Rate, Yield);
%calculate all greeks for a Call using NAG
[NAG_Call, NAG_CallDelta, NAG_Gamma, NAG_Vega, NAG_CallTheta, NAG_CallRho, NAG_CallCrho, NAG_CallVanna,...
NAG_CallCharm, NAG_CallSpeed, NAG_CallColour,NAG_CallZomma, NAG_CallVomma, ifail] = ...
s30ab('c', Strike, Price, Time, Volatility, Rate, Yield);
%Calculate the Elasticity (Lambda)
NAG_CallLambda = Price/NAG_Call*NAG_CallDelta;
NAG_PutLambda = Price/NAG_Put*NAG_PutDelta;
%Calculate the same set of prices and greeks using the MATLAB Finance Toolbox
[MW_Call, MW_Put] = blsprice(Price, Strike, Rate, Time, Volatility, Yield);
[MW_CallDelta, MW_PutDelta] = blsdelta(Price, Strike, Rate, Time, Volatility, Yield);
MW_Gamma = blsgamma(Price, Strike, Rate, Time, Volatility, Yield);
MW_Vega = blsvega(Price, Strike, Rate, Time, Volatility, Yield);
[MW_CallTheta, MW_PutTheta] = blstheta(Price, Strike, Rate, Time,Volatility, Yield);
[MW_CallRho, MW_PutRho]= blsrho(Price, Strike, Rate, Time, Volatility,Yield);
[MW_CallLambda,MW_PutLambda]=blslambda(Price, Strike, Rate, Time, Volatility,Yield);
Note that NAG doesn’t output the elasticity (Lambda) directly but it is trivial to obtain it using values that it does output. Also note that as far as I can tell, NAG outputs more Greeks than the Financial Toolbox does.
I’m not going to show the entire output of the above program because there are a lot of numbers. However, here are the Put values as calculated by NAG shown to 4 decimal places. I have checked and they agree with the Financial Toolbox to within numerical noise.
NAG_Put =0.1350 NAG_PutDelta =-0.0419 NAG_PutLambda =-15.5066 NAG_Gamma =0.0119 NAG_Vega =2.2361 NAG_PutTheta =-1.1187 NAG_PutRho =-0.5572 NAG_PutCrho = -0.5235 NAG_PutVanna =-0.4709 NAG_PutCharm =0.2229 NAG_PutSpeed =-0.0030 NAG_PutColour =-0.0275 NAG_PutZomma =0.0688 NAG_PutVomma =20.3560
I recently got access to a shiny new (new to me at least) set of compilers, The Portland PGI compiler suite which comes with a great set of technologies to play with including AVX vector support, CUDA for x86 and GPU pragma-based acceleration. So naturally, it wasn’t long before I wondered if I could use the PGI suite as compilers for MATLAB mex files. The bad news is that The Mathworks don’t support the PGI Compilers out of the box but that leads to the good news…I get to dig down and figure out how to add support for unsupported compilers.
In what follows I made use of MATLAB 2012a on 64bit Windows 7 with Version 12.5 of the PGI Portland Compiler Suite.
In order to set up a C mex-compiler in MATLAB you execute the following
mex -setup
which causes MATLAB to execute a Perl script at C:\Program Files\MATLAB\R2012a\bin\mexsetup.pm. This script scans the directory C:\Program Files\MATLAB\R2012a\bin\win64\mexopts looking for Perl scripts with the extension .stp and running whatever it finds. Each .stp file looks for a particular compiler. After all .stp files have been executed, a list of compilers found gets returned to the user. When the user chooses a compiler, the corresponding .bat file gets copied to the directory returned by MATLAB’s prefdir function. This sets up the compiler for use. All of this is nicely documented in the mexsetup.pm file itself.
So, I’ve had my first crack at this and the results are the following two files.
These are crude, and there’s probably lots missing/wrong but they seem to work. Copy them to C:\Program Files\MATLAB\R2012a\bin\win64\mexopts. The location of the compiler is hard-coded in pgi.stp so you’ll need to change the following line if your compiler location differs from mine
my $default_location = "C:\\Program Files\\PGI\\win64\\12.5\\bin";
Now, when you do mex -setup, you should get an entry PGI Workstation 12.5 64bit 12.5 in C:\Program Files\PGI\win64\12.5\bin which you can select as normal.
An example compilation and some details.
Let’s compile the following very simple mex file, mex_sin.c, using the PGI compiler which does little more than take an elementwise sine of the input matrix.
#include <math.h>
#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
double *in,*out;
double dist,a,b;
int rows,cols,outsize;
int i,j,k;
/*Get pointers to input matrix*/
in = mxGetPr(prhs[0]);
/*Get rows and columns of input*/
rows = mxGetM(prhs[0]);
cols = mxGetN(prhs[0]);
/* Create output matrix */
outsize = rows*cols;
plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL);
/* Assign pointer to the output */
out = mxGetPr(plhs[0]);
for(i=0;i<outsize;i++){
out[i] = sin(in[i]);
}
}
Compile using the -v switch to get verbose information about the compilation
mex sin_mex.c -v
You’ll see that the compiled mex file is actually a renamed .dll file that was compiled and linked with the following flags
pgcc -c -Bdynamic -Minfo -fast pgcc --Mmakedll=export_all -L"C:\Program Files\MATLAB\R2012a\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib
The switch –Mmakedll=export_all is actually not supported by PGI which makes this whole setup doubly unsupported! However, I couldn’t find a way to export the required symbols without modifying the mex source code so I lived with it. Maybe I’ll figure out a better way in the future. Let’s try the new function out
>> a=[1 2 3]; >> mex_sin(a) Invalid MEX-file 'C:\Work\mex_sin.mexw64': The specified module could not be found.
The reason for the error message is that a required PGI .dll file, pgc.dll, is not on my system path so I need to do the following in MATLAB.
setenv('PATH', [getenv('PATH') ';C:\Program Files\PGI\win64\12.5\bin\']);
This fixes things
>> mex_sin(a)
ans =
0.8415 0.9093 0.1411
Performance
I took a quick look at the performance of this mex function using my quad-core, Sandy Bridge laptop. I highly doubted that I was going to beat MATLAB’s built in sin function (which is highly optimised and multithreaded) with so little work and I was right:
>> a=rand(1,100000000); >> tic;mex_sin(a);toc Elapsed time is 1.320855 seconds. >> tic;sin(a);toc Elapsed time is 0.486369 seconds.
That’s not really a fair comparison though since I am purposely leaving mutithreading out of the PGI mex equation for now. It’s a much fairer comparison to compare the exact same mex file using different compilers so let’s do that. I created three different compiled mex routines from the source code above using the three compilers installed on my laptop and performed a very crude time test as follows
>> a=rand(1,100000000); >> tic;mex_sin_pgi(a);toc %PGI 12.5 run 1 Elapsed time is 1.317122 seconds. >> tic;mex_sin_pgi(a);toc %PGI 12.5 run 2 Elapsed time is 1.338271 seconds. >> tic;mex_sin_vs(a);toc %Visual Studio 2008 run 1 Elapsed time is 1.459463 seconds. >> tic;mex_sin_vs(a);toc Elapsed time is 1.446947 seconds. %Visual Studio 2008 run 2 >> tic;mex_sin_intel(a);toc %Intel Compiler 12.0 run 1 Elapsed time is 0.907018 seconds. >> tic;mex_sin_intel(a);toc %Intel Compiler 12.0 run 2 Elapsed time is 0.860218 seconds.
PGI did a little better than Visual Studio 2008 but was beaten by Intel. I’m hoping that I’ll be able to get more performance out of the PGI compiler as I learn more about the compilation flags.
Getting PGI to make use of SSE extensions
Part of the output of the mex sin_mex.c -v compilation command is the following notice
mexFunction:
23, Loop not vectorized: data dependency
This notice is a result of the -Minfo compilation switch and indicates that the PGI compiler can’t determine if the in and out arrays overlap or not. If they don’t overlap then it would be safe to unroll the loop and make use of SSE or AVX instructions to make better use of my Sandy Bridge processor. This should hopefully speed things up a little.
As the programmer, I am sure that the two arrays don’t overlap so I need to give the compiler a hand. One way to do this would be to modify the pgi.dat file to include the compilation switch -Msafeptr which tells the compiler that arrays never overlap anywhere. This might not be a good idea since it may not always be true so I decided to be more cautious and make use of the restrict keyword. That is, I changed the mex source code so that
double *in,*out;
becomes
double * restrict in,* restrict out;
Now when I compile using the PGI compiler, the notice from -Mifno becomes
mexFunction:
23, Generated 3 alternate versions of the loop
Generated vector sse code for the loop
Generated a prefetch instruction for the loop
which demonstrates that the compiler is much happier! So, what did this do for performance?
>> tic;mex_sin_pgi(a);toc Elapsed time is 1.450002 seconds. >> tic;mex_sin_pgi(a);toc Elapsed time is 1.460536 seconds.
This is slower than when SSE instructions weren’t being used which isn’t what I was expecting at all! If anyone has any insight into what’s going on here, I’d love to hear from you.
Future Work
I’m happy that I’ve got this compiler working in MATLAB but there is a lot to do including:
- Tidy up the pgi.dat and pgi.stp files so that they look and act more professionally.
- Figure out the best set of compiler switches to use– it is almost certain that what I’m using now is sub-optimal since I am new to the PGI compiler.
- Get OpenMP support working. I tried using the -Mconcur compilation flag which auto-parallelised the loop but it crashed MATLAB when I ran it. This needs investigating
- Get PGI accelerator support working so I can offload work to the GPU.
- Figure out why the SSE version of this function is slower than the non-SSE version
- Figure out how to determine whether or not the compiler is emitting AVX instructions. The documentation suggests that if the compiler is called on a Sandy Bridge machine, and if vectorisation is possible then it will produce AVX instructions but AVX is not mentioned in the output of -Minfo. Nothing changes if you explicity set the target to Sandy Bridge with the compiler switch –tp sandybridge–64.
Look out for more articles on this in the future.
Related WalkingRandomly Articles
- Which MATLAB functions make use of multithreading?
- Using Intel’s SPMD Compiler (ispc) with MATLAB on Linux
- Parallel MATLAB with OpenMP mex files
- MATLAB mex functions using the NAG C Library
My setup
- Laptop model: Dell XPS L702X
- CPU: Intel Core i7-2630QM @2Ghz software overclockable to 2.9Ghz. 4 physical cores but total 8 virtual cores due to Hyperthreading.
- GPU: GeForce GT 555M with 144 CUDA Cores. Graphics clock: 590Mhz. Processor Clock:1180 Mhz. 3072 Mb DDR3 Memeory
- RAM: 8 Gb
- OS: Windows 7 Home Premium 64 bit.
- MATLAB: 2012a
- PGI Compiler: 12.5
As the resident MATLAB support guy (among other things) at The University of Manchester, I often get asked which language MATLAB is written in. Many people seem to argue over whether or not it is Fortran or C with the prevailing wisdom being ‘It was originally written in Fortran for the free version 1 and then was re-written in C when it went commercial’.
Well, I am not sure about the history but looking at moden MATLAB, the question should be which languages, rather than which language! The list I have so far is
- C (many built in compiled mex functions are written in C)
- C++ (MATLAB makes use of the Boost C++ libraries..thanks to M in the comments for this one)
- CIL (Common Intermediate Langauge, used to be called MSIL, – The windows version of MATLAB uses this for various .NET stuff. Thanks to ‘pmcs’ in the comments for this one)
- NVidia CUDA (Routines in the GPU part of the parallel computing toolbox)
- Bash shell script (On Linux, the startup ‘binary’ is a shell script)
- Fortran (MATLAB uses the MKL and I’m fairly sure this is written in Fortran)
- Java (Many of the ticks in Yair Altman’s excellent book, Undocumented Secrets of MATLAB-Java Programming
make use of the Java elements in MATLAB
- MATLAB (many MATLAB functions are written in .m code)
- Perl (Many mex-related scripts are written in Perl)
- Windows batch files (I’ve seen some simple .bat files as part of the mex machinery)
The MATLAB language has become ubiquitous in many fields of applied mathematics such as linear algebra, differential equations, control systems and signal processing among many others. MATLAB is a great tool but it also costs a lot! If you are not a student then MATLAB is a very expensive piece of software. For example, my own academic licensed copy with just 4 toolboxes cost more than the rather high powered laptop I use it on. If I left academia then there would be no chance of me owning a copy unless I found an employer willing to stump up the cash for a commercial license. Commercial licenses cost a LOT more than academic licenses.
Octave – The free alternative
The good news is that there is a free alternative to MATLAB in the form of Octave. Octave attempts to be source compatible with MATLAB which means that, in many cases, your MATLAB code will run as-is on Octave. Many of the undergraduate courses taught at my university (The University of Manchester) could be taught using Octave with little or no modification and I imagine that this would be the case elsewhere. One area where Octave falls down is in the provision of toolboxes but this is improving thanks to the Octave-Forge project.
Addi – The beginnings of MATLAB/Octave on Android
As Dylan said The Times They Are a-Changin’ and there is an ever-increasing segment of world-society that are simply skipping over the PC and going straight to mobile devices for their computing needs. It is possible to get your hands on a functional Android mobile phone or tablet for significantly less than the cost of a PC. These cheap mobile devices may be a lot less powerful than even the cheapest of PCs but they are powerful enough for many purposes and are perfectly capable of outgunning Cray supercomputers from the past.
There is, however, no MATLAB for Android devices. The best we have right now is in the form of Addi, a free Android app that makes use of JMathLib to provide a very scaled-back MATLAB-like experience. Addi is the work of Corbin Champion, an android developer from Portland in the US, and he has much bigger plans for the future.
Full Octave/GNUPlot on Android with no caveats
Corbin is working on a full Octave and GNUPlot* port for Android. He has already included a proof of concept in the latest release of Addi which includes an experimental Octave interpreter. To go from this proof of concept to a fully developed Android port, however, is going to take a lot of work. Corbin is up to the task but he would like our help.
[* – GNUPLot is used as the plotting engine for Octave and includes support for advanced 3D graphics]
Donate as little as $1 to help make this project possible
Corbin has launched a Kickstarter project in order to try to obtain funding for this project. He freely admits that he’ll do the work whether or not it gets funded but will be able to devote much more of his time to the project if the funding request is successful. After all, we all need to eat, even great sotware developers.
Although I have never met him, I believe in Corbin and strongly believe that he will deliver on his promise. So much so that I have pledged $100 to the project out of my own pocket.
If, like me, you want to see a well-developed and supported version of Octave on Android then watch the video below and then head over to Corbin’s kickstarter page to get the full details of his proposal. The minimum donation is only $1 and your money will only be taken if the full funding requirement is met.
Update (16th May 2012): The project (and this post) made it to Slashdot :)
A MATLAB user at Manchester recently sent me some code and data as part of a Condor support request but Outlook helpfully blocked access to his .mat files deeming them potentially unsafe! It seems that Microsoft Outlook is afraid of matrices.
I guess that the easiest way to proceed would be to ask him to resend his stuff as a .zip file but that would slow down the support query. I just wanted to stop Outlook being quite so paranoid and let me have access to those scary matrices.
The fix is a registry edit (very user-friendly of Microsoft right?) so please don’t proceed if you are not comfortable editing the registry. The registry controls numerous aspects of the Windows operating system along with many of the applications you have installed. If it had a map then there would be huge areas labelled ‘Here be Dragons.’ Corrupting the registry could lead to an unusable operating system. So, the following is offered without warranty on a ‘it works for me’ basis.
Open up regedit and navigate to the following (I’m using Windows 7 and Outlook 2007, location and procedure may be different for different environments).
HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Security
Once you have the above highlighted, move your mouse to the right hand pane of regedit, and right click to get a context menu. Click on New->String Value and create a key-value pair with
- Name Level1Remove
- Value Data .mat
If you want to unblock more than just .mat then you can separate them with semicolons so .mat;.exe for example. When you’ve finished, this part of the registry should look like the screenshot below

MATLAB uses the Intel Math Kernel Library (MKL) behind the scenes to perform many linear algebra operations. I wondered what version of the MKL was being used by MATLAB 2012a on my 64 bit Windows machine. The most straightforward way to determine this is to simply ask MATLAB:
>> version -lapack ans = Intel(R) Math Kernel Library Version 10.3.5 Product Build 20110720 for Intel(R) 64 architecture applications Linear Algebra PACKage Version 3.3.1 >> version -blas ans = Intel(R) Math Kernel Library Version 10.3.5 Product Build 20110720 for Intel(R) 64 architecture applications
Thanks to The Mathworks technical support team for this information.
I recently installed MATLAB 2012a on a Windows machine along with a certain set of standard Mathworks toolboxes. In addition, I also installed the excellent NAG Toolbox for MATLAB which is standard practice at my University.
I later realised that I had not installed all of the Mathworks toolboxes I needed so I fired up the MATLAB installer again and asked it to add the missing toolboxes. This extra installation never completed, however, and gave me the error message
The application encountered an unexpected error and needed to close. You may want to try re-installing your product(s). More infomation can be found at C:\path_to_a_log_file
I took a look at the log file mentioned which revealed a huge java error that began with
java.util.concurrent.ExecutionException: java.lang.StringIndexOutOfBoundsException:
String index out of range: -2
at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at javax.swing.SwingWorker.get(Unknown Source)
at com.mathworks.wizard.worker.WorkerImpl.done(WorkerImpl.java:33)
at javax.swing.SwingWorker$5.run(Unknown Source)
A little mucking around revealed that the installer was unhappy with the pathdef.m file at C:\Program Files\MATLAB\R2012a\toolbox\local\pathdef.m
The installer for the NAG Toolbox modifies this file by adding the line
'C:\Program Files\MATLAB\R2012a\toolbox\NAG\mex.w64;' ...
near the beginning and the lines
'C:\Program Files\MATLAB\R2012a\help\toolbox\NAG;' ... 'C:\Program Files\MATLAB\R2012a\help\toolbox\NAGToolboxDemos;' ...
Near the end and it seems that the MATLAB installer really doesn’t like this. So, what you do is create a copy of this pathdef.m file (pathdef.m.old for example) and then remove the non-mathworks lines in pathdef.m. Now you can install the extra Mathworks toolboxes you want. Once the installer has finished its work you can re-add the non-mathworks lines back into pathdef.m using your copy as a guide.
I’ll be informing both NAG and The Mathworks about this particular issue but wanted to get this post out there as soon as possible to provide a workaround since at least one other person has hit this problem at my University and I doubt that he will be the last (It’s also going to make SCCM deployment of MATLAB a pain but that’s another story).
Update
The Mathworks technical support have sent me a better workaround than the one detailed above. What you need to do is to change
'C:\Program Files\MATLAB\R2012a\toolbox\NAG\mex.w64;' ...
to
'C:\Program Files\MATLAB\R2012a\toolbox\NAG\mex.w64;', ...
The Mathworks installer is unhappy about the missing comma.
