Skip to content

Instantly share code, notes, and snippets.

@muschellij2
Created April 21, 2020 16:00
Show Gist options
  • Save muschellij2/1918b860c9961551ee75f02bfedef474 to your computer and use it in GitHub Desktop.
Save muschellij2/1918b860c9961551ee75f02bfedef474 to your computer and use it in GitHub Desktop.
Read MATLAB File
read_mat = function(mat) {
L = try({
R.matlab::readMat(mat)
})
if (!inherits(L, "try-error")) {
return(L)
}
names = rhdf5::h5ls(mat)$name
convert_mat_string = function(x) {
rawToChar(as.raw(x))
}
L = lapply(names, function(x) {
print(x)
out = rhdf5::h5read(file = mat, name = x)
out_attrs = rhdf5::h5readAttributes(mat, name = x)
MATLAB_class = out_attrs$MATLAB_class
if (!is.null(MATLAB_class)) {
if (MATLAB_class == "char") {
out = convert_mat_string(out)
}
}
out
})
names(L) = names
return(L)
}
read_acc_mat = function(mat, check_names = TRUE) {
names = rhdf5::h5ls(mat)$name
if (check_names) {
stopifnot(all(c("Xi", "fs", "hed", "startdate", "starttime") %in%
names))
}
L = read_mat(mat)
colnames(L$Xi) = c("X", "Y", "Z")
L$Xi = tibble::as_tibble(L$Xi)
L
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment