MATLAB: LibGL error unable to load driver in Ubuntu 16.04 while running Matlab R2013b

I was trying to run a m-file on R2013b in Ubuntu 16.04. But i got the error and Matlab just hung:
libGL error: unable to load driver:
libGL error: driver pointer missing
libGL error: failed to load driver: r600
libGL error: unable to load driver:
libGL error: failed to load driver: swrast
The m-file tries to draw 3D plots of point clouds after reading ply files and processing them. My Matlab IDE completely froze and i had to use xkill to close it as nothing else worked.
The same code works normally in Windows without any extra configuration. By adding breakpoints and running one by one line, i found that the Matlab's plot3() function is giving trouble. I have previously done 2D plots using plot() function and it worked seamlessly in Ubuntu matlab. Both the files and are in /usr/lib/x86_64-linux-gnu/dri.
What is the purpose of these shared object files? Why plot() does not give error and plot3() does? What does libGL error mean? And how to resolve it?
There are other places that try to address similar problems but they are in different context and difficult for me to extrapolate for my purposes. I would like an answer specific to my problem.
Most unexpectedly, i have also noted that this problem does not happen in R2016a because MATLAB selects its own graphics rendering engine as software opengl and sets it up automatically for its purpose.

Best Answer

Note - According to MathWorks' System Requirement and Availability document, Ubuntu 16.04 is not fully tested with any versions of MATLAB, so it is not recommended to use Ubuntu 16.04 with MATLAB.
Also, this question is similar to this question
For reference: r600 is the name of the Hardware driver provided by AMD Radeon. The Radeon driver is already pre-installed on Ubuntu. The r600 driver used for Hardware OpenGL. While swrast driver is just used in Software OpenGL.
As for your issue:
  • plot3() does 3-D rendering, and plot() does 2-D rendering.
  • MATLAB is shipped with a version of "libstdc++" (a system standard library) to ensure that there is a minimum amount of functionality in the library to support MATLAB
  • This is done by inserting the location of our "libstdc++" in the library search path before the system version’s location.
  • The issue arises when we pull in libraries that we haven’t built (graphics drivers being one of them), that rely on a version of "libstdc++" that is a newer version than the one we ship. In all likelihood, the graphics driver has expectations of entry points and functionality that may not exist in our older version of "libstdc++".
Potential workaround:
If this situation occurs, rename the file found at " $MATLAB_ROOT$/sys/os/glnxa64/" to "", restart MATLAB, this forces MATLAB to use the OS library.
$MATLABROOT can be found out by typing in the command "matlabroot" in your MATLAB Command Window.