How to save (serialize) MATLAB variables to a byte string?
34 views (last 30 days)
Show older comments
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.
5 Comments
Walter Roberson
on 7 Jun 2025
fid = fopen('test.txt', 'w');
fprintf(fid, 'hello\n');
ftell(fid)
objectByteArray = getByteStreamFromArray(fid);
fclose(fid)
fid = getArrayFromByteStream(objectByteArray)
ftell(fid)
Yair Altman
on 7 Jun 2025
Edited: Yair Altman
on 7 Jun 2025
The file ID returned by fopen() is a numeric integer that is automatically allocated by the operating system. It is a sort of a pointer index to the file in the OS's list of open files (or -1 upon error) - in your example the value 3 (values 0-2 are pre-selected for stdout,stdin and stderr respectively). This pointer data is ephemeral, so although it can be serialized (just as any other integer value), it has no meaning upon deserialization. Importantly, the file ID is not an object that stores internal properties that could be serialized/deserialized, and it doesn't include the file contents or meta-data. On the other hand, if you use object representations of files (e.g. System.IO.File or java.io.File), you should be able to serialize them excluding their internal ephemeral properties.
Stated in other words, even in your example, serialization and deserialization of the file ID worked as expected, in the sense that it serialized and restored the integer value "3". The interpretation of this value as belonging to an open file is not something that is inherently stored in the integer value, and so it cannot be restored. As with any programming function or engineering tool, it is up to the user to ensure that they understand what the function/tool is doing and what its limitations are. Specifically, serializing ephemeral data, while technically possible, results in garbage. Also, deserializing data requires the user to reinterpret any data which is not inherently deserialized.
Accepted Answer
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
Yair Altman
on 31 May 2020
Edited: Yair Altman
on 31 May 2020
Here's a detailed discussion/explanation of the undocumented interface: http://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data
More Answers (0)
See Also
Categories
Find more on Logical 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!