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

36 vues (au cours des 30 derniers jours)
Kirill
Kirill le 10 Déc 2016
Commenté : Kirill le 16 Déc 2016
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 commentaires
Guillaume
Guillaume le 16 Déc 2016
@Steven,
There are two parallel things:
  1. 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.
  2. 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.
Kirill
Kirill le 16 Déc 2016
For example, try this in Matlab 2014b:
x=network;
y=getByteStreamFromArray(x);
z=getArrayFromByteStream(y);
You will have an error. It's unsupported function, so we have to be aware of what we are doing ;)

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 14 Déc 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.
  2 commentaires
Kirill
Kirill le 16 Déc 2016
Guillaume and Walter.
Thanks. It will works.
What I'm thinking it's really using a RAM disk or just a cached file system and use standard and supported save / read / load functions. I will try to use getByteStreamFromArray and getArrayFromByteStream, but changing version of Matlab can be a problem.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by