Add write dmat and off functions.
Tested.