matlab 使用R阅读.mat文件

wfypjpf4  于 2023-03-30  发布在  Matlab
关注(0)|答案(2)|浏览(256)

我正在尝试使用R读取.mat文件。

library(R.matlab)
data <- readMat('e-060RAW.mat')

它给了我这个错误。

Error in readMat5(con, firstFourBytes = firstFourBytes, maxLength = maxLength) : 
  Reading of MAT v7.3 files is not supported. If possible, save the data in MATLAB using 'save -V6'.

我该怎么解决这个问题呢?有没有其他方法可以用R来读取一个.mat文件?

jv4diomz

jv4diomz1#

https://www.rdocumentation.org/packages/R.matlab/versions/3.6.2/topics/readMat开始:
MAT v7.3文件,例如save('foo.mat', '-v7.3')保存,以分层数据格式(HDF 5)[6,7]存储数据,该格式不受本函数/包支持,但存在其他R包可以解析HDF 5,例如CRAN包h5和Bioconductor包rhdf 5。

w8ntj3qf

w8ntj3qf2#

使用hdf5r包阅读.mat(V7.3,matlab〉= 2006b)文件

## install.packages("hdf5r")
  library(hdf5r)
  ## ######################################################################
  ## create an hdf5 file
  test_file <- tempfile(fileext=".h5")
  file.h5 <- H5File$new( test_file, mode="w")

  data(cars)
  cars
  file.h5$create_group("test")
  file.h5[["test/cars"]] <- cars
  cars_ds <- file.h5[["test/cars"]]
  cars_ds
  h5attr(cars_ds, "rownames") <- rownames(cars)

  ## Close the file at the end
  ## the 'close' method closes only the file-id, but leaves object inside the file open
  ## This may prevent re-opening of the file. 'close_all' closes the file and all objects in it
  file.h5$close_all()

  ## read and investigate the hdf5 file
  file.h5 <- H5File$new(test_file, mode="r+")

  ## lets look at the content
  file.h5$ls(recursive=TRUE)

  cars_ds <- file.h5[["test/cars"]]
  ## note that for now tables in HDF5 are 1-dimensional, not 2-dimensional
  mycars <- cars_ds[]
  h5attr_names(cars_ds)
  h5attr(cars_ds, "rownames")

  file.h5$close_all()

相关问题