I had the same problem as dario.
The Name of the Layer was "0 " instead of "0". Therefore, i changed line 12 to:
"fprintf(fid,'LINE\n8\n0\n');"
Now it works perfect for me.
(Maybe a little late...)
Thanks for the script, very useful. however I tried to use the function but when opening in AutoCad 2011 it says
Invalid or incomplete DXF input -- drawing discarded.
even though t picks the coordinates (I can see them in the dxf file opened with notepad). Do you know wht's wrong?