Added a try/catch block to prevent the code from hanging in matlab