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
io_read_mat
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()
ieegio::io_write_mat(list(x = x), con = f)
read_mat(f)
#> Using native approach to load the matlab file (supporting MAT 5.0)...
#> $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: Sat Feb 15 19:28:49 2025 "
#>
#> 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[]
}
#> /var/folders/2s/h6hvv9ps03xgz_krkkstvq_r0000gn/T//Rtmp64Vhbn/file1e4a340d981d => x (Dataset Created)
#> /var/folders/2s/h6hvv9ps03xgz_krkkstvq_r0000gn/T//Rtmp64Vhbn/file1e4a340d981d => x (Dataset Removed)
#> /var/folders/2s/h6hvv9ps03xgz_krkkstvq_r0000gn/T//Rtmp64Vhbn/file1e4a340d981d => x (Dataset Created)
#> [,1] [,2] [,3] [,4]
#> [1,] 1 5 9 13
#> [2,] 2 6 10 14
#> [3,] 3 7 11 15
#> [4,] 4 8 12 16