Your does work properly here (although it doesn't show anything useful …)
Did you try to use the debugger? What is the contents of L when the error occurs?
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!