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

조회 수: 43 (최근 30일)
Brandon Barker
Brandon Barker 2020년 5월 26일
편집: Yair Altman 2020년 5월 31일
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.

채택된 답변

Mohammad Sami
Mohammad Sami 2020년 5월 27일
There is an undocumented function called "getByteStreamFromArray" which converts a matlab object to bytestream.
To convert it back use the opposite function "getArrayFromByteStream".
  댓글 수: 2
Brandon Barker
Brandon Barker 2020년 5월 27일
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.
Yair Altman
Yair Altman 2020년 5월 31일
편집: Yair Altman 2020년 5월 31일
Here's a detailed discussion/explanation of the undocumented interface: http://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by