A compatible reader that can read both 'Matlab' files prior and after version 6.0
Arguments
- file
path to a 'Matlab' file
- ram
whether to load data into memory. Only available when the file is in 'HDF5' format. Default is false and will load arrays, if set to true, then lazy-load data. This is useful when array is very large.
- engine
method to read the file, choices are
'r'
and'py'
('Python'); if'py'
is chosen, make sureconfigure_conda
is configured.
Details
readMat
can only read 'Matlab' files
prior to version 6. After version 6, 'Matlab' uses 'HDF5' format
to store its data, and read_mat
can handle both cases.
The performance of read_mat
can be limited when
the file is too big or has many datasets as it reads all the
data contained in 'Matlab' file into memory.
Examples
# Matlab .mat <= v7.3
x <- matrix(1:16, 4)
f <- tempfile()
R.matlab::writeMat(con = f, x = x)
read_mat(f)
#> $x
#> [,1] [,2] [,3] [,4]
#> [1,] 1 5 9 13
#> [2,] 2 6 10 14
#> [3,] 3 7 11 15
#> [4,] 4 8 12 16
#>
#> attr(,"header")
#> attr(,"header")$description
#> [1] "MATLAB 5.0 MAT-file, Platform: unix, Software: R v4.4.2, Created on: Fri Dec 20 17:41:23 2024 "
#>
#> attr(,"header")$version
#> [1] "5"
#>
#> attr(,"header")$endian
#> [1] "little"
#>
# Matlab .mat >= v7.3, using hdf5
# Make sure you have installed hdf5r
if( dipsaus::package_installed('hdf5r') ){
f <- tempfile()
save_h5(x, file = f, name = 'x')
read_mat(f)
# For v7.3, you don't have to load all data into RAM
dat <- read_mat(f, ram = FALSE)
dat
dat$x[]
}
#> [,1] [,2] [,3] [,4]
#> [1,] 1 5 9 13
#> [2,] 2 6 10 14
#> [3,] 3 7 11 15
#> [4,] 4 8 12 16