RF: refactor matlab endian fixes

Fix endian check for writing arrays.
Simplfy code from fix for unicode endianness on read.
Add tests.