Loren on the Art of MATLAB

Turn ideas into MATLAB

R2007a Released 35

Posted by Loren Shure,

Release 2007a came out today. This corresponds to MATLAB version 7.4. I will list here a few of my favorite new MATLAB features. I will discuss some of them in future blogs.



Multithreaded Computational Support

MATLAB has multithreaded computation support for many linear algebra and element-wise numeric operations, allowing performance
improvement on multicore and multiprocessor systems.

M-Lint in the Editor

When using the MATLAB Editor, for certain messages about your code, you now have the option, through a right-click menu, to
replace the code with the recommendation.

Utility to Help with Parsing Inputs

When writing a function that needs to be robust, there's often a lot of code up front to validate the inputs. inputParser can now help you with that in a more methodical way.

Function for Singleton Expansion

In February 2006, I posted an article on Scalar Expansion and More that generated a lot of interest. See the new function bsxfun (for Binary Singleton Expansion Function) to help perform binary operations on element-by-element functions for arrays which
agree in sizes in all dimensions unless the one of the different sizes is a scalar.

Try R2007a

I've only listed a few of the latest features in MATLAB. What's your favorite new feature? Tell people about it here.

Get the MATLAB code

Published with MATLAB® 7.4


Comments are closed.

35 CommentsOldest to Newest

Mike replied on : 1 of 35


One of the features I am most interested in is the Multithreaded Computational Support. Is there a list anywhere that gives full details of exactly which functions have gained multi-thread support?


Happy User replied on : 2 of 35

Thanks for the hard work that went into the latest release.

bsxfun is by far the new feature that will have the greatest impact on my work. My work requires a lot of simulation and to keep things vectorized, I am required to work in the 3d and 4th dimensions. bsxfun will save me from a lot of repmatting. The code be cleaner, faster, and require less memory.

The other new feature that has a lot of use is the categorical and datasets arrays in the stat toolbox.

Thanks again.

Dan K replied on : 3 of 35

Hi there,
I’ve got a quick question regarding some of the new mLint behavior. It keeps telling me that cd is not supported by MCC. Now obviously it is supported…. I compiled one just to make sure, but is this a warning of things to come? Likewise, there is a warning about matlabroot, not being supported in deployed apps.

Todd Flanagan replied on : 4 of 35

Hi Dan,

The language in that m-lint warning is probably too strong. “Not supported” should probably be interpreted as “not a good idea” and a source of problems if you use those commands.

More specifically, when someone uses MATLABROOT in a deployed app, they should often be using CTFROOT. MATLABROOT will point to the MCR installation, which will be shared among all compiler generated apps and may point at a MATLAB install or an MCR install. This depends on what has been installed on the machine and the specific order of things on the path. CTFROOT points to the base directory where the compiled app’s ctf file has been extracted. This is often what the developer is really after when building up a path. Often when someone uses MATLABROOT to build up a path, they are looking for a work directory or a specific toolbox. Neither of these things exist off MATLABROOT in a compiled app. This is really the purpose of that warning.

CD is troublesome in a compiled app because the end user can install the app in an unexpected place and start it from a different directory than the one in which it was installed. Most uses of CD in general m-code don’t account for this all that well, so we point it out in m-lint as a bad idea. More importantly, because compiled apps only work with encrypted m-files, cd-ing to a directory with unencrypted m-files won’t make those files accessible and can lead to unexpected behavior like “I know my m-files are right there! Why won’t you use them.” CD is, however, appropriate for making data files available to compiled apps.

Both of these things are compilable and useful is used with discretion under the right circumstances.


Dan K replied on : 5 of 35

Thanks for the input. I can see the reasons why those two warnings are in there, but neither applies in this case. I use matlabroot to determine an absolute path to the ghostscript executable for converting postscript to pdf, as it is the only way I have found for printing multiple figures to a multipage pdf.
The cd that I use is to allow many computers to access data repositories on a company server, and has nothing to do with where the executable resides.
As a side question, though, is there any way to identify the directory containing the .exe and .ctf files when you are running an executable, specifically to find a parameters file which is co-located. I tried ctfroot, but that pointed to someplace deep inside the mcr folder. Thanks,

Happy User replied on : 6 of 35

I would think that _pwd_ would give you the location of the exe and ctf when running.

Todd Flanagan replied on : 7 of 35

Hi Dan,

PWD may give you the right answer, but only in some circumstances. CTFROOT should point to the directory where the ctf file has been extracted and is the value I think you want to use. For example:

H:\Documents>set PATH=%PATH%;d:\temp\whereami

Extracting CTF archive. This may take a few seconds, depending on the
size of your application. Please wait…
…CTF archive extraction complete.
ctfroot: d:\temp\whereami\whereami_mcr
pwd : H:\Documents


In general, the ctf and exe will be located in the directory below this. In this case d:\temp\whereami. It is possible that the ctf and exe are not colocated, but this is unusual. You should be able to do something like:

if isdeployed
open(fullfile(ctfroot, ‘..’,’mydata.mat’));

Drop me a note at tflanagan at mathworks dot com if this isn’t working for you.


Loren replied on : 9 of 35

Hi Darik-

I will write a blog on this topic soon. Part of it was implemented in R2006b, some more in R2007a, with more to come in the future. For now, if you have a function where the output name is the same as one of the input’s AND is itself called from another function AND it uses “safe” operations such as +, the calculations will be done in place now. Some of those restrictions may be relaxed further over time.


Dan K replied on : 10 of 35

Thanks for the information. I’m a bit swamped right now, so I can’t try it yet, but I will certainly try it asap. Thanks again.

Darik Gamble replied on : 11 of 35

Thanks Loren.

I understand that Mathworks is trying to simplify the programming process by insulating the end-user from some of the uglier memory-management issues, but it would be nice to have it better documented.

Looking forward to your blog article on the topic.

Tim Davis replied on : 12 of 35

Regarding the “x = myfun(x)” method for saving memory.

This is wonderful! Can it be exploited by a mexFunction?

One idea would be for MATLAB to preassign the plhs array with pointers to any variables that also appear in the prhs array. Then, a mexFunction could check:

if (plhs [0] == prhs [0]) …

and if true, then this mxArray could be modified in place by the mexFunction.

MexFunctions that do not recognize this trick will go ahead and allocate a new array and assign the plhs [0] pointer, signifying (on return to MATLAB) that a new copy has been made. So this technique would be backward compatible with existing mexFunctions.

This is essential in methods that would like to modify just part of a matrix. A sparse “cholupdate” can work in time proportional to the number of entries that change in L, which can be as few as O(1). That’s much much less than nnz(L). Currently, there’s no way to write a mexFunction that does this work in O(1) time, because it requires that the mexFunction modify its input.

Now, mexFunctions can cheat by modifying their inputs anyway; you can write a mexFunction that does (in M pseudocode):

function mangle(x)
x (1) = 42 ;

and changes x in the caller. Of course, that’s exceedingly dangerous if x is an expression (what does “mangle(x-1)” do?), or if x is a shallow copy:

x = y
mangle (x)

would also modify y.

If a safe alternative were available, then “x = mangle(x)” could be written as a mexFunction, and it could recognize that x appears as both the input and output, and avoid making a copy.

Tim Davis replied on : 13 of 35

Regarding the multithreading question: I hope this is done with care. Can MATLAB be selective, using threads for some BLAS but not others? Some functions that call the BLAS are faster with multitheading, but some are slower. Much slower.

The multithreaded BLAS authors (Intel, AMD, Goto, etc.) all share one common problem: they ignore the performance of the multithreaded BLAS on small matrices. For small matrices, a single thread is much faster.

So who should care if small matrices are slower? If you work with dense matrices, you don’t care. However, x=A\b when A is square makes many many calls to the BLAS, with lots of small to medium to large dense matrices (the matrices are dense submatrices of the larger sparse matrix; using multifrontal for LU and supernodal for chol).

With lots of calls to the BLAS for small matrices, x=A\b can be very slow with multithreading, even for rather large sparse matrices.

One would think that inside the BLAS there could be a test. If the dimensions of A and B for C=A*B (in dgemm) were small, then the BLAS should skip the work in forking off a thread, and just do the work in one thread. As it is, unless you use x=A\b on sparse matrices arising from large 3D discretizations, you should avoid multithreading for sparse matrix computations.

Loren replied on : 14 of 35


There is no mexFunction API for inplace operations right now. It is on the enhancement list.


Bobby Cheng replied on : 15 of 35


Thank you for bring up such an important topic.

Your concern on the performance of small matrix computations is justified. What you suggested is a great short term solution to the problem by avoiding it altogether.

Most of the vendor BLASes have already built-in logics to selectively ignore the default multithreading setting. Unfortunately, the logic are not always optimal. The long term solution is to work with vendors to fix the logic and push for high performance both the single-threaded and multithreaded BLASes for small matrix computations.

In MATLAB, the wording in the preference panel is very carefully chosen. You are selecting the maximum number of computational threads and not the number of computational threads. MATLAB are free to ignore the number if the threading overhead proved to be too great.


Bo replied on : 16 of 35


I’m using Tim Myers “MATLAB OLEDB Connection and Query Functions” for different commands against MS SQL databases. I have not investigated this very deep, but after installing 2007a it is extreem slow! This has been tested on another “clean” machine with same result. What has changed in this version?


Loren replied on : 17 of 35


Please see the link to the release notes in the post itself. If you don’t see anything relevant, please contact technical support with an example of different behavior.


Luca Balbi replied on : 18 of 35

One of the features I like best is the inputParser object (you know, I’m writing code that other people will use without looking at the arguments!!!). By the way, I noticed that the inputParser object supports the “object.method” syntax, for instance you can have ip.AddRequired(…).
Is it possible to implement such a behaviour (more intuitive to the user) in user-defined MATLAB object (documentation apparently says no)?
I mean, if p is a ‘polynom’ object, p.plot(0:0.1:1) sounds better than plot(p,0:0.1:1)…

Thanks a lot and keep up the great work, I really like your blog Loren!

Loren replied on : 19 of 35


Thanks for the note. In fact, you can use dot notation with user objects but you have to overload subsref for the class in order to achieve this. I don’t have any examples myself to share at the moment.


per isakson replied on : 20 of 35


Tom Krauss presented a solution in the CSSM-thread “THIS (or SELF) object in matlab” (Date: 2005-11-23 08:56:48).


Luca Balbi replied on : 21 of 35

I’d like to thank Loren for her kind reply and Per for the link, which is a good example I’m going to try out soon.

michele coletti replied on : 22 of 35

Hi Loren I am using matlab 7.4, I was tesing the multithreading and while it works perfectly on the “inv” function it does not seem to work with matrix vector multiplication.
If I do something like


the time elapsed is always the same, am I doing something wrong?

Loren replied on : 23 of 35


You don’t say what kind of machine you run on. Is it a multicore or multiprocessor? Have you changed the preferences? What happens when you run the multithreaded demo?

Talking to developers involved here, they say that matrix-vector multiply will not speed up because it is dominated by data access, similar to .*. However, you should see a speed up with matrix-matrix multiply. Level 3 BLAS are the main beneficiaries of multithreading. The BLAS level 1 and 2 are dominated by data access typically. These are common results if you read sites for hardware manufacturers.


michele replied on : 24 of 35

I have a pc with two opteron dual-core.
I have tried and it works fine with matrix by matrix multiplications. I did not know that matrix by vector multiplications were not improved by multithreading.
Thank you very much

David replied on : 25 of 35

Is there anyway of switching on multithreading from command line? I don’t use GUI (and to be honest, getting that running each time would be a pain) and would like to enable multithreading, but so far it seems it is only possible from the preference pane.

Any advice?


Loren replied on : 26 of 35


You can only switch the multithreading through the GUI, not the command line in R2007a. You may be able to get programmable control in future releases.


Cédric replied on : 30 of 35


I read in this topic that we are not selecting the number of computational threads but the maximum number of computational threads in the GUI preferences.
I would like to know if the use of the function “setNumberOfComputationalThreads(N)” allow us to select the exact number of thread to use.
In addition, I made benchmarks on matrix-matrix operations with sparse matrix or not (addition, subtraction, multiplication…). I obtained performance improvement on my dual core with matrix-matrix multiplication only ( and not with sparse matrix ).
I know that Intel MKL/BLAS library allows to use multithread with all of this operations ( and on sparse matrix too). Is matlab able to use all mkl functions ?

Thank you in advance for your response,

Bobby Cheng replied on : 31 of 35

First setNumberOfComputationalThreads is not a supported function. In R2007b, MATLAB provides maxNumCompThreads to change the maximum number of computational threads programmatically.

Again, this function only provides the ability to set the maximum number of computational threads. Can you tell us why this is important in your work? I think even if we provided such function, MATLAB built-in function would not react to this setting, because a) vendor BLAS has their own threading algorithms that MATLAB has not control of, and b) many computation will slow down if we don’t take care how to/whether to multithread.

One good example is sparse in bench.m. You did not observe speed up for sparse becuase it involves a lot of BLAS calls to smaller matrices and vendor BLAS decided it will not multithread. In fact, many customer has observed slow down with sparse in bench.m when multithreading is turned on. This turned out that vendor BLAS are not universally faster with multithreading on. In fact, the multithreaded BLAS can be slower on particular matrice size. Here is one of the many reasons why the default setting for multithreading is off for R2007a and R2007b. We are working with vendors to plug these holes.

The long term goal obviously is to turn on multithreaded computation by default. All users need to know is that MATLAB will take advantage of their latest multi-core chips effectively.

Kamran Turkoglu replied on : 32 of 35

I need your help related with Multithreading issue of Matlab R2007b.

We just purchased a Quad Core2Duo work station (in total 8 cores), on which we would like to run some simulations. What we would like to do is to see from Windows Task Manager that all 8 cores are working with full speed on our simulations? Is it possible? And if it is possible, how?

We enabled Multithreading property of Matlab R2007b from preferences and it shows us that it is able to use 8 cores for computational efforts. But what we see from Windows Task Manager’s CPU Performance window is that only 1 of the 8 cores is making a peak, and the others are sleeping.

How can we activate all cores to work with their maximum performance at the same time in order to run our simulation under Matlab R2007b?

Best wishes and regards,
Yours respectfully,

Alacon AV replied on : 33 of 35


I’d like to know whether Matlab allows you to program using threads, like java. I need to run two M-Files at the same time. Thanks, Best wishes,


Loren replied on : 34 of 35


There is no way to program directly with threads using M-files in a single MATLAB session. You might consider our Parallel Computing Toolbox, depending on what you are trying to do.


Ken Atwell replied on : 35 of 35

Alacon AV —

If you are interested in seeing programmatic threading in MATLAB, drop me a line (I am a product manager for MATLAB). I would love to hear what you have in mind. My email address is:

ken dot atwell at mathworks dot com