Wednesday, December 22, 2010

isdeployed( )

isdeployed() is a handy function in matlab to check whether a piece of matlab code is running as a standalone deployed app or whether it is running in native matlab.

Tuesday, June 29, 2010

Truncating or rounding off a decimal value/array to user-specified number of decimal places

Sometimes, you want to truncate long floating point numbers to keep just the first few digits following the decimal point. The easy way to do this is

xr = round(x/n) * n

where
x = original floating point number
n = 10^(-[number of digits after decimal])

e.g. x=1.5673454, n = 0.01 (2 digits after decimal point)
xr = 1.57

Saturday, April 17, 2010

Passing data in and out of MATLAB and Python

Came across this great package that allows direct exchange between MATLAB and Python.

Monday, April 5, 2010

How to solve MCR cache access problems on a cluster

Often when I run compiled matlab applications on a cluster, I get the error message

"Could not access the MCR component cache."

This tends to happen because matlab is not able to access the MCE cache directory. By default this happens to be your home directory. When a large number of compiled matlab programs are starting off/running simultaneously (e.g. you submit a job array), the load on the file system is too great giving rise to the problem.

The simplest way to solve this problem, if to point the MCR_CACHE_ROOT environment variable to a local temporary directory on each node on the cluster.

export MCR_CACHE_ROOT=$TMPDIR

This redirects the cache to a temp directory that is able to handle the traffic.

Saturday, January 9, 2010

High density scatter plots

The scatter(x,y) function in MATLAB is useful to visualize the joint distribution of two variables x and y. But this function breaks down (gets too slow and memory intensive) if the number of data points in x/y is large.

A nice trick to visualize high density scatter plots is to bin the data and smooth the 2-D histogram. Then one can use the image function or surf function with alpha transparency to view the joint distribution. Darker regions could represent high density of points and light regions could represent low density of points.

R and several other programming languages have built in functions of this. It is a little surprising that MATLAB doesn't have it built in yet. Anyway, here is a paper that gives a very efficient way of creating these smoothed high-density scatter plots and here is an implementation.