title: Is it possible to intercept a matlab save() bytestream
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>A few clarifications:</p>
  
  <ol>
  <li><p>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.</p></li>
  <li><p>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.</p></li>
  </ol>
  
  <p>Complications:</p>
  
  <ul>
  <li><p>It seems that the functionality used in the <code>save</code> 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 <code>matPutVariable</code> (previously called <code>matPutArray</code>) which writes a given variable of type <code>mxArray*</code> out to a file of type <code>MATFile*</code> opened with <code>matOpen</code>. The problem here is the following text in the description of <code>matPutVariable</code>: </p>
  
  <blockquote>
    <p>If the <code>mxArray</code> does not exist in the MAT-file, the function appends it to the end. If an <code>mxArray</code> with the same name exists in the file, the function replaces the existing <code>mxArray</code> with the new <code>mxArray</code> by rewriting the file.</p>
  </blockquote>
  
  <p>This means that the <code>matPutVariable</code> 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.</p></li>
  </ul>