0

I have located a .MAT file that contains data that I wish to view in .CSV format, and I do not have a MATLAB license.

Happily, there is an R.matlab package, with a readMat function that enables reading .MAT files into R.

Unhappily, the product of applying readMat to my .MAT file is not a data frame, and I've been struggling all morning trying to figure out how to convert the readMat output into a data frame so that I can export the data as a .CSV file.

Here's the start of my R script:

library(R.matlab)
library(tidyr)

cats <- readMat("cats.mat")

head(cats)

And here's what I'm seeing on my Rstudio console:

> cats <- readMat("cats.mat")
> 
> head(cats)
$catlist
, , 1

            [,1]         
catname     List,10500  
catgroup    Numeric,10500
flag        Numeric,10500

$catdate
     [,1]         
[1,] "01-Dec-2020"

I'd really appreciate help converting the readMat output into a dataframe (with columns: catname, catgroup, and flag)!! Thanks! :)

sharleen
  • 23
  • 6
  • Have you tried `as.data.frame(cats[[1]])`? If that doesn't work, can you provide a link to `cats.mat`? – Ian Campbell Jan 05 '21 at 17:04
  • 1
    @Ian Campbell - Thank you!! A combination of those worked (it created column names that contained the entire contents of each column -- starting with "structure.c." -- but the data were extracted in the desired format, and that's all I needed). I really appreciate your time and expertise! – sharleen Jan 05 '21 at 17:35
  • Glad it worked for you. Maybe consider answering your own question with what worked for you. – Ian Campbell Jan 05 '21 at 17:35
  • Will do - working on that now. – sharleen Jan 05 '21 at 17:39

1 Answers1

1

Solution (thanks to Ian Campbell):

Note: The column names are wonky, but the data were successfully extracted and exported into a .CSV file!

library(R.matlab)
library(tidyr)

cats <- readMat("cats.mat")

cats2 <- cats$catlist

cats3 <- lapply(cats2, unlist, use.names=TRUE)

cats_for_export <- as.data.frame(cats3)

write.csv(cats_for_export, file="cats.csv")
sharleen
  • 23
  • 6