[SciPy-dev] Patch for writing Matlab structs with long field names

Vebjorn Ljosa ljosa@broad.mit....
Fri Dec 5 17:57:43 CST 2008

There is a bug in io/matlab/mio5.py: it can only write structs the file
names of which are shorter than 32 characters.  I am passing on a patch
from Lee Kamentsky <leek@broad.mit.edu> that extends this to the 64
characters, which is what Matlab can handle.


Index: mio5.py
--- mio5.py	(revision 5227)
+++ mio5.py	(working copy)
@@ -808,9 +808,11 @@
     def write_fields(self):
         # write fieldnames
         fieldnames = [f[0] for f in self.arr.dtype.descr]
-        self.write_element(np.array([32], dtype='i4'))
-        self.write_element(np.array(fieldnames, dtype='S32'),
-                           mdtype=miINT8)
+        length = max([len(fieldname) for fieldname in fieldnames])+1
+        if length > 64:
+            raise ValueError, "Field names are restricted to 64 characters in Matlab"
+        self.write_element(np.array([length], dtype='i4'))
+        self.write_element(np.array(fieldnames, dtype='S%d'%(length)), mdtype=miINT8)
         A = np.atleast_2d(self.arr).flatten('F')
         MWG = Mat5WriterGetter(self.file_stream,

More information about the Scipy-dev mailing list