In matlab it is possible to write matlab objects, or even the entire workspace, to a file using the matlab save() call. I would like to intercept the bytestream and postprocess it before it goes to a file, is this possible? Alternatively, is it possible to specify the filedescriptor that the bytestream is written to instead of the filename that usually goes into the save() call as an argument.
Note that I'm not looking for an alternative way to write a file in matlab, I know I can fopen() a file and write whatever I want, but the point is that I want to (re)use the object serialization that is internal to the save call, not invent my own again.
An analog question would of course arise for the load() call, but in that case intercepting the bytestream before it goes into the deserialization process, but I guess if it is possible for save() the solution to the load() problem will follow naturally.
A few clarifications:
I'm not looking at a new way to serialize matlab data, it already exists and the whole point of the exercise is to use the existing serialization in the save() call so that 1) I don't need to start updating the serialization code for new types of objects in newer versions of matlab, or heaven forbid people start using custom OOP objects, and 2) I can still easily use existing code to read in mat files, such as for example scipy's support for mat files.
The stream must not get out to a file or anything before post-processing, the idea is encryption for security, writing the stream out plain to a file completely undermines that purpose.
Complications:
It seems that the functionality used in the save
function in matlab isn't just a regular sequential write. Examining the object code of the libraries it seems that the save function is implemented using matPutVariable
(previously called matPutArray
) which writes a given variable of type mxArray*
out to a file of type MATFile*
opened with matOpen
. The problem here is the following text in the description of matPutVariable
:
If the mxArray
does not exist in the MAT-file, the function appends it to the end. If an mxArray
with the same name exists in the file, the function replaces the existing mxArray
with the new mxArray
by rewriting the file.
This means that the matPutVariable
function will have to seek through the file, obviously seeking will not be possible when pipes are used, so using pipes to implement our processing of the bytestream is not possible when using this existing serialization functionality.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…