Title: | Read and Write 'Matlab' Files |
---|---|
Description: | Read and write 'Matlab' MAT files from R. The 'rmatio' package supports reading MAT version 4, MAT version 5 and MAT compressed version 5. The 'rmatio' package can write version 5 MAT files and version 5 files with variable compression. |
Authors: | Stefan Widgren [aut, cre] (Author of the R interface to the C-library matio), Christopher Hulbert [aut] (Author of the C-library matio, http://sourceforge.net/projects/matio/) |
Maintainer: | Stefan Widgren <[email protected]> |
License: | GPL-3 |
Version: | 0.18.0.9000 |
Built: | 2024-11-02 05:58:06 UTC |
Source: | https://github.com/stewid/rmatio |
Reads the values in a mat-file to a list.
read.mat(filename)
read.mat(filename)
filename |
Character string, with the MAT file or URL to read. |
Reads the values in a mat-file and stores them in a list.
A list with the variables read.
A sparse complex matrix is read as a dense complex matrix.
A sparse logical matrix is read as a 'lgCMatrix'
A sparse matrix is read as a 'dgCMatrix'
A matrix of dimension 1 x n
or n x 1
is read
as a vector
A structure is read as a named list with fields.
A cell array is read as an unnamed list with cell data
A function class type is read as NULL and gives a warning.
See write.mat
for more details and
examples.
## Read a version 4 MAT file with little-endian byte ordering filename <- system.file("extdata/matio_test_cases_v4_le.mat", package = "rmatio") m <- read.mat(filename) ## View content str(m) ## Read a version 4 MAT file with big-endian byte ordering. filename <- system.file("extdata/matio_test_cases_v4_be.mat", package = "rmatio") m <- read.mat(filename) ## View content str(m) ## Read a compressed version 5 MAT file filename <- system.file("extdata/matio_test_cases_compressed_le.mat", package = "rmatio") m <- read.mat(filename) ## View content str(m)
## Read a version 4 MAT file with little-endian byte ordering filename <- system.file("extdata/matio_test_cases_v4_le.mat", package = "rmatio") m <- read.mat(filename) ## View content str(m) ## Read a version 4 MAT file with big-endian byte ordering. filename <- system.file("extdata/matio_test_cases_v4_be.mat", package = "rmatio") m <- read.mat(filename) ## View content str(m) ## Read a compressed version 5 MAT file filename <- system.file("extdata/matio_test_cases_compressed_le.mat", package = "rmatio") m <- read.mat(filename) ## View content str(m)
Reading and writing Matlab MAT files from R
rmatio
supports reading MAT version 4, MAT version 5 and
MAT compressed version 5.
rmatio
can write version 5 MAT files and version 5 files
with variable compression.
The MathWorks Inc., MATLAB - MAT-File Format, version
R2013b, September 2013.
https://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf
Writes the values in a list to a mat-file.
write.mat(object, filename = NULL, compression = TRUE, version = c("MAT5")) ## S4 method for signature 'list' write.mat(object, filename = NULL, compression = TRUE, version = c("MAT5"))
write.mat(object, filename = NULL, compression = TRUE, version = c("MAT5")) ## S4 method for signature 'list' write.mat(object, filename = NULL, compression = TRUE, version = c("MAT5"))
object |
The |
filename |
The MAT file to write. |
compression |
Use compression when writing variables. Defaults to TRUE. |
version |
MAT file version to create. Currently only support for Matlab level-5 file (MAT5) from rmatio package. |
Writes the values in the list to a mat-file. All values in the list must have unique names.
invisible NULL
A vector is saved as a 1 x length
array
Support for writing a sparse matrix of type 'dgCMatrix' or 'lgCMatrix' to file
Stefan Widgren
## Not run: library(Matrix) filename <- tempfile(fileext = ".mat") ## Example how to read and write an integer vector with rmatio write.mat(list(a = 1:5), filename = filename) a <- as.integer(read.mat(filename)[["a"]]) stopifnot(identical(a, 1:5)) unlink(filename) ## Read a compressed version 5 MAT file m <- read.mat(system.file("extdata/matio_test_cases_compressed_le.mat", package = "rmatio")) ## Write an uncompressed version 5 MAT file write.mat(m, filename = "test-uncompressed.mat", compression = FALSE, version = "MAT5") ## Write a compressed version 5 MAT file write.mat(m, filename = "test-compressed.mat", compression = TRUE, version = "MAT5") ## Check that the content of the files are identical identical(read.mat("test-uncompressed.mat"), read.mat("test-compressed.mat")) unlink("test-uncompressed.mat") unlink("test-compressed.mat") ## Example how to read and write a S4 class with rmatio ## Create 'DemoS4Mat' class setClass("DemoS4Mat", representation(a = "dgCMatrix", b = "integer", c = "matrix", d = "numeric")) ## Create a function to coerce a 'DemoS4Mat' object to a list. setAs(from = "DemoS4Mat", to = "list", def = function(from) { return(list(a = from@a, b = from@b, c = from@c, d = from@d)) } ) ## Create a function to coerce a list to a 'DemoS4Mat' object. setAs(from = "list", to = "DemoS4Mat", def = function(from) { new("DemoS4Mat", a = from[["a"]], b = as.integer(from[["b"]]), c = from[["c"]], d = from[["d"]]) } ) ## Define a method to write a 'DemoS4Mat' object to a MAT file. setMethod("write.mat", signature(object = "DemoS4Mat"), function(object, filename, compression, version) { ## Coerce the 'DemoS4Mat' object to a list and ## call 'rmatio' 'write.mat' with the list. write.mat(as(object, "list"), filename, compression, version) } ) ## Create a new 'DemoS4Mat' object demoS4mat <- new("DemoS4Mat", a = Matrix(c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), nrow = 3, ncol = 9, byrow = TRUE, sparse = TRUE), b = 1:5, c = matrix(as.numeric(1:9), nrow = 3), d = c(6.0, 7.0, 8.0)) ## Write to MAT file write.mat(demoS4mat, filename) ## Read the MAT file demoS4mat_2 <- as(read.mat(filename), "DemoS4Mat") ## Check result stopifnot(identical(demoS4mat, demoS4mat_2)) unlink(filename) ## End(Not run)
## Not run: library(Matrix) filename <- tempfile(fileext = ".mat") ## Example how to read and write an integer vector with rmatio write.mat(list(a = 1:5), filename = filename) a <- as.integer(read.mat(filename)[["a"]]) stopifnot(identical(a, 1:5)) unlink(filename) ## Read a compressed version 5 MAT file m <- read.mat(system.file("extdata/matio_test_cases_compressed_le.mat", package = "rmatio")) ## Write an uncompressed version 5 MAT file write.mat(m, filename = "test-uncompressed.mat", compression = FALSE, version = "MAT5") ## Write a compressed version 5 MAT file write.mat(m, filename = "test-compressed.mat", compression = TRUE, version = "MAT5") ## Check that the content of the files are identical identical(read.mat("test-uncompressed.mat"), read.mat("test-compressed.mat")) unlink("test-uncompressed.mat") unlink("test-compressed.mat") ## Example how to read and write a S4 class with rmatio ## Create 'DemoS4Mat' class setClass("DemoS4Mat", representation(a = "dgCMatrix", b = "integer", c = "matrix", d = "numeric")) ## Create a function to coerce a 'DemoS4Mat' object to a list. setAs(from = "DemoS4Mat", to = "list", def = function(from) { return(list(a = from@a, b = from@b, c = from@c, d = from@d)) } ) ## Create a function to coerce a list to a 'DemoS4Mat' object. setAs(from = "list", to = "DemoS4Mat", def = function(from) { new("DemoS4Mat", a = from[["a"]], b = as.integer(from[["b"]]), c = from[["c"]], d = from[["d"]]) } ) ## Define a method to write a 'DemoS4Mat' object to a MAT file. setMethod("write.mat", signature(object = "DemoS4Mat"), function(object, filename, compression, version) { ## Coerce the 'DemoS4Mat' object to a list and ## call 'rmatio' 'write.mat' with the list. write.mat(as(object, "list"), filename, compression, version) } ) ## Create a new 'DemoS4Mat' object demoS4mat <- new("DemoS4Mat", a = Matrix(c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), nrow = 3, ncol = 9, byrow = TRUE, sparse = TRUE), b = 1:5, c = matrix(as.numeric(1:9), nrow = 3), d = c(6.0, 7.0, 8.0)) ## Write to MAT file write.mat(demoS4mat, filename) ## Read the MAT file demoS4mat_2 <- as(read.mat(filename), "DemoS4Mat") ## Check result stopifnot(identical(demoS4mat, demoS4mat_2)) unlink(filename) ## End(Not run)