MATLAB: How to convert any Matlab variable into vector of bytes (decode/encode)

bytesdecodeencodeMATLABobjects

Hi!
I'm trying to store Matlab results/objects that I got in MongoDB. It's simple to do when you have data types such as Double / Char etc. But when you have an object (for example neural network) you need to make some tricks.
I want to create the simplest serializer from byte vector to object. What I can do, I can save a specific variable to *.mat file and then read it, and then write byte vector to the MongoDB. But it is an ugly solution, I need to make an intermediate write operation to a disk.
Do we have any conversion from Matlab variable to set of bytes and back?
x = network;
y = encodebytes(x) % <- generates from x to vector of bytes
z = decodebytes(y) % <- generates from vector of bytes to new object
isequal(x,z)
ans = 1
Thank you!

Best Answer

There are no official method of serialising / deserialising objects in matlab, other than saving them in a mat file and storing the content of that file (sloooow)
There are the completely undocumented getByteStreamFromArray and getArrayFromByteStream functions. A much more thorough answer can be found on Yair Altman's blog.
Of course, since the functions are completely undocumented, there's always the risk they'll be removed in a future version. Use at your own peril.