MATLAB: Am I not able to write a sparse matrix to a file using the DLMWRITE function in MATLAB 7.1 (R14SP3)

dlmwriteMATLABmatricesmatrixsparsesprintf

When I execute the following commands:
s=sparse(eye(5,5));
dlmwrite('mydata.dat',s);
I receive the following result:
??? Error using ==> sprintf
Function is not defined for sparse inputs.
Error in ==> dlmwrite at 172
str = sprintf(format,m(i,:));
However, the above commands works without errors in MATLAB 6.5.1 (R13SP1) and a file 'mydata.dat' is created.

Best Answer

This is not a bug. The error message you are receiving is issued by the SPRINTF function, which does not accept sparse matrices as input. To work around this issue, convert the sparse matrix to a full matrix before using DLMWRITE. Here are the steps:
s=sparse(eye(5,5));
dlmwrite('mydata.dat',full(s));