good example from http://www.mathworks.co.uk/matlabcentral/fileexchange/authors/55260