MATLAB Answers

How to save (serialize) MATLAB variables to a byte string?

7 views (last 30 days)
Brandon Barker
Brandon Barker on 26 May 2020
Edited: Yair Altman on 31 May 2020
Ideally this would be using the same format as the save command (.mat file), but instead of writing to disk directly, it would write it to data in memory.
The reason is I need to access the saved data directly to implement a custom hashing method on the variable, and have no need to write it to disk in order to achieve this, and would rather not go through the administratively-heavy option of creating a ram disk through ramfs.
I'd actually be OK with a version-change of MATLAB invalidating the object in my particular case (the idea being that any software updates may influence results, however unlikely).
Assuming full .mat support is not an option, I still need fairly general support, which is why the (intermediate) output of save seems like a good option: e.g., nested structs, cells, arrays. I could probably do without Java objects in this particular case.

  0 Comments

Sign in to comment.

Accepted Answer

Mohammad Sami
Mohammad Sami on 27 May 2020
There is an undocumented function called "getByteStreamFromArray" which converts a matlab object to bytestream.
To convert it back use the opposite function "getArrayFromByteStream".

  2 Comments

Brandon Barker
Brandon Barker on 27 May 2020
Very nice. If this is indeed the underlying function used by save (and maybe even if it isn't), I'd like to request Mathworks consider making it a documented function. This sort of thing can be quite useful, and even if the exact output isn't stable, as long as the type signature is stable I hope that would be enough.

Sign in to comment.

More Answers (0)