1

I'd like to convert some MAT files to XML (each .mat file contains a hierarchical structure) so I can access the data from other software (like R, Python). These MAT files were generated in Matlab 6.5 so they are not in the HDF5 data format specification. There is a user-contributed XML toolbox (from 2003) which suggests a simple conversion from MAT to XML is possible:

http://www.mathworks.com/matlabcentral/fileexchange/4278

but I seem to run into an error with the xml_format function:

??? NaN's cannot be converted to logicals.

The data structure I am trying to convert is nested on many levels with many data types (arrays, etc.), so I may have to write my own exporting function. I am using Matlab Version 7.10.0.499 (R2010a) 64-bit (maci64) so I should have the xmlwrite and xmlread functions available to me:

http://www.mathworks.com/help/techdoc/import_export/f5-86078.html#bsmnj5u

However, when I try to follow their example,

docNode = com.mathworks.xml.XMLUtils.createDocument('root_element')

I get an error:

??? Undefined variable "com" or class

If I try to see which java classes are loaded, I get

[M,X,J] = inmem;
J = 

    'MException'

though in my $matlabroot/toolbox/local/classpath.txt there appear to be a lot of Java classes loaded.

Do I need to install Java modules or add additional paths, or something else to get this to working? Or do you have any suggestions to go from MAT to another common data exchange format that allows arbitrary nesting of structures (some of which contain array representation of images)? I have not seen much in the way of MAT to HDF5 conversion, which seems like it could also be a reasonable alternative to going from MAT to XML... Thanks ~

2
  • Have you done modifications on classpath.txt? The 'com' error seems to be an issue with your MATLAB installation. With a different MATLAB version, but on the same platform, I could not reproduce your error. Commented Jan 24, 2011 at 7:03
  • @zellus -- no, I haven't made any modifications... should I? Commented Jan 24, 2011 at 23:25

1 Answer 1

2

Since newer mat files are just HDF5 files with rows/cols flipped you could just write new matlabs to get it into HDF5.

To convert the old mat files to the newer HDF5 format just open them in matlab and resave

s=open('AFile.mat')
save('AFile.mat','-struct','s')

or so

You can also explore the hdf5write function

Sign up to request clarification or add additional context in comments.

4 Comments

Ah, that's a simple solution... thanks. But there is an error reading the file as an hdf5 file in R -- may not be that simple?
hmm. Try saving it with the '-v6' option. I think ML gzips all mlmatrices in the mat file w/o that option. Anyway, look at hdf5write too
The '-v6' option didn't work either, but 'hdf5write' seems to do the trick... Thanks much.
save -v7.3 filename Should do it as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.