How to convert any Matlab variable into vector of bytes (decode/encode)
36 views (last 30 days)
Show older comments
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!
14 Comments
Guillaume
on 16 Dec 2016
@Steven,
There are two parallel things:
- The versioning of the object (developed long before matlab knew how to talk to subversion), which indeed has nothing to do with a database. That works mostly fine, the object stores properly which version of the code was used to create it. The only time this partially breaks down is when you try to load some very old versions of objects with newer code because loadobj is not capable enough.
- The objects are function objects used to batch process tons of engine tests. The results of the processing are stored in a database with test conditions, etc. Together with the results, I want to store all the parameters used to create them, that is: the state of the function objects. For that, I need a way to serialise/deserialise the object. That's not currently supported by matlab.
Accepted Answer
Guillaume
on 14 Dec 2016
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.
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!