A <- matrix(1:27, ncol = 3)
B <- as.matrix(1:10)
C <- array(1:18, dim = c(2, 3, 3))
filename <- paste(tempfile(), ".mat", sep = "")
writeMat(filename, A = A, B = B, C = C)
data <- readMat(filename)
str(data)
X <- list(A = A, B = B, C = C)
stopifnot(all.equal(X, data[names(X)]))
unlink(filename)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# All objects written must be named uniquely
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
tryCatch({
# Named
writeMat(filename, A = A)
# Not named
writeMat(filename, A)
}, error = function(ex) {
cat("ERROR:", ex$message, "\n")
})
tryCatch({
# Uniquely named
writeMat(filename, A = A, B = B, C = C)
# Not uniquely named
writeMat(filename, A = A, B = B, A = C)
}, error = function(ex) {
cat("ERROR:", ex$message, "\n")
})
if (FALSE) {
# When writing to a stream connection the receiver needs to know on
# beforehand how many bytes are available. This can be done by using
# the 'onWrite' argument.
onWrite <- function(x)
writeBin(x$length, con = x$con, size = 4, endian = "little")
writeMat(con, A = A, B = B, onWrite = onWrite)
}
Run the code above in your browser using DataLab