Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Help reading a particular .mat file into R

So I've been trying to read this particular .mat file into R. I don't know too much about matlab, but I know enough that the R.matlab package can only read uncompressed data into R, and to save it as uncompressed I need to save it as such in matlab by using save new.mat -v6.

Okay, so I did that, but when I used readMat("new.mat") in R, it just got stuck loading that forever. I also tried using package hdf5 via:

> hdf5load("new.mat", load=FALSE)->g
Error in hdf5load("new.mat", load = FALSE) : 
  can't handle hdf type 201331051

I'm not sure what this problem could be, but if anyone wants to try to figure this out the file is located at http://dibernardo.tigem.it/MANTRA/MANTRA_online/Matlab_Code%26Data.html and is called inventory.mat (the first file).

Thanks for your help!

like image 730
JoshDG Avatar asked Oct 31 '11 15:10

JoshDG


People also ask

Can R read .MAT files?

The files in this dataset are in Matlab's . mat format. Fortunantly, we can easily read them into R using the R.

How do I import MATLAB data into R?

Importing MATLAB Files into R You can use the R. matlab package with its readMat() function to import MATLAB files into R. The readMat() function will return a named list structure that contains all variables from the MAT file that you imported.

How do I read a .MAT file?

How to Open an MAT File. MAT files that are Microsoft Access Shortcut files can be created by dragging a table out of Access and to the desktop or into another folder. Microsoft Access needs to be installed in order to use them. MATLAB from MathWorks can open MAT files that are used by that program.

How do I save a .MAT file as a variable?

To save a subset of your workspace variables to a MAT-file, select the variables in the Workspace browser, right-click, and then select Save As. You also can drag the selected variables from the Workspace browser to the Current Folder browser.


2 Answers

This particular file has one object, inventory, which is a struct object, with a lot of different things inside of it. Some are cell arrays, others are vectors of doubles or logicals, and a couple are matrices of doubles. It looks like R.matlab does not like cells arrays within structs, but I'm not sure what's causing issues for R to load this. For reasons like this, I'd generally recommend avoiding mapping structs in Matlab to objects in R. It is similar to a list, and this one can be transformed to a list, but it's not always a good idea.

I recommend creating a new file, one for each object, e.g. ids = inventory.instance_ids and save each object to either a separate .mat file, or save all of them, except for the inventory object, into 1 file. Even better is to go to text, e.g via csvwrite, so that you can see what's being created.

I realize that's going around use of a Matlab to R reader, but having things in a common, universal format is much more useful for reproducibility than to acquire a bunch of different readers for a proprietary format.

Alternatively, you can pass objects in memory via R.matlab, or this set of functions + the R/DCOM interface (on Windows).

Although this doesn't address how to use R.matlab, I've done a lot of transferring of data between R and Matlab, in both directions, and I find that it's best to avoid .mat files (and, similarly, .rdat files). I like to pass objects in memory, so that I can inspect them on each side, or via standard text files. Dealing with application specific file formats, especially those that change quite a bit and are inefficient (I'm looking at you MathWorks), is not a good use of time. I appreciate the folks who work on readers, but having a lot more control over the data structures used in the target language is very much worth the space overhead of using a simple output file format. In-memory data transfer is very nice because you can interface programs, but that may be a distraction if your only goal is to move data.

like image 92
Iterator Avatar answered Sep 20 '22 23:09

Iterator


Have you run the examples in http://cran.r-project.org/web/packages/R.matlab/R.matlab.pdf on pages 22 to 24? That will test your ability to read from versions 4 and 5. I'm not sure that R cannot read compressed files. There is an Rcompresssion package in Omegahat.

like image 37
IRTFM Avatar answered Sep 23 '22 23:09

IRTFM