R.matlab
R.matlab copied to clipboard
Field names are not pulled
Hi! Great work,man!
I am importing a Matlab structure into R using readMat
, and my issue is that the field names were not imported. I included the option fixNames=TRUE
.
Thanks!
Thanks. I'm glad you like it.
I take it that you had a problem and found a solution to his.
I found a workaround, but I wonder if it is a bug or not. In general, are a structure's field names generally pulled too?
I see.
fixNames=TRUE
replaces underscores (_
) in variable names with periods (.
).
That was needed in the past when R didn't support underscores in variable
names. These days it shouldn't be needed when reading in MAT files to R.
So, I'm surprise that fixNames=TRUE
solved your problem reading in a MAT
file. If you can make a MAT file available to me that has this problem and
that is solved by fixNames=TRUE
, I can have look at it. It's really
really useful to me if you can provide me with a minimal MAT file, because
I need to go through the logs of everything read internally in order
troubleshoot this. You can try yourself to see if you find something
particular from:
data <- readMat(pathname, verbose=-200)
The output is long. You can send it to file by:
R.utils::withSink({
data <- readMat(pathname, verbose=-200)
}, file="readMat.log", type="message")
Thanks,
Henrik
On Wed, Feb 4, 2015 at 10:44 AM, Yenny Webb-Vargas <[email protected]
wrote:
I found a workaround, but I wonder if it is a bug or not. In general, are a structure's field names generally pulled too?
— Reply to this email directly or view it on GitHub https://github.com/HenrikBengtsson/R.matlab/issues/10#issuecomment-72912914 .
Hi Henrik!
The .mat file has only one structure in it. When I import it, the name of the structure is present, but the name of its fields is not. Here is the output file:
Opens binary file: ~/GitHub/functional_mediation/data_application/meta.mat R.matlab options... R.matlab::readMat/rawBufferSize: 1e+07 R.matlab::readMat/rawBufferMethod: 1.8.0 R.matlab::readMat/decompressWith: memDecompress R.matlab::readMat/onDecompressError: error R.matlab options...done Trying to read MAT v5 file stream. Read MAT v5 header: $description [1] "MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Wed Jan 28 09:58:06 2015 "
$version [1] "5"
$endian [1] "little"
Endian: little Reading data element... Reading Tag... Compressed tag: FALSE type signed.miCOMPRESSED sizeOf.miCOMPRESSED what "miCOMPRESSED" NA NA NA nbrOfBytes padding compressed "1951268" "4" "FALSE" Decompressing 1951268 bytes zraw [1951268 bytes; compression type: zlib]: 78, 9c, ec, 92, c1, 4e, 84, 30, ..., 97, 5c, 1a, 00, 6a, a2, 4d, 68
Sorry, this one slipped off my radar. Since it's been so long, I did have enough details to reproduce this (problematic MAT file would be required) and there's been several updates since then, I'm choosing to close this issue.
Please reopen if this is still an issue. Thanks for the feedback.
Hi Henrik — thanks for the nice package!
I believe OP was referring to the name of the nested lists. I don't have a minimal example, but here's a comparison with rmatio::read.mat() for a file I'm working with:
x_rmatlab <- R.matlab::readMat("x.mat")
x_rmatio <- rmatio::read.mat("x.mat")
> str(x_rmatlab, max.level = 2)
List of 1
$ gep:List of 6
..$ :List of 19198
..$ : num [1, 1:23] 408 117 468 42 131 626 33 185 166 522 ...
..$ :List of 23
..$ :List of 460
..$ : num [1:19198, 1:23] 1.18 1.06 0.17 1.91 NaN ...
..$ : num [1:19198, 1:23] 2.72e-01 4.31e-01 4.34e-15 8.16e-02 NaN ...
..- attr(*, "dim")= int [1:3] 6 1 1
..- attr(*, "dimnames")=List of 3
- attr(*, "header")=List of 3
..$ description: chr "MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Fri Nov 23 22:48:05 2018 "
..$ version : chr "5"
..$ endian : chr "little"
> str(x_rmatio, max.level = 2)
List of 1
$ gep:List of 6
..$ genes :List of 19198
..$ N :List of 1
..$ cancer_type:List of 23
..$ tcga_id :List of 460
..$ fc :List of 1
..$ pv :List of 1
I know that the drop
argument unnest some lists, but in x_rmatlab
, we don't see the names "genes", "N", "cancer_type", etc.
Hi, thanks for the example. Is there any chance you could share that file to be added as a test file to the package? Ideally also with as small elements as possible to save space.
Can't promising a fix soon but, in order to fix things, we need to be able to reproduce and add package tests for it so it won't break again later.
Hi Henrik — unfortunately, I cannot share the file. I don't currently have Matlab on my machine either (and honestly it's been a while since I last worked with .mat files), so I'm not sure if I can create a minimal example from Matlab for you.
What I tried is creating a .mat file from R using rmatio::read.mat(). I'm not sure if this x
object is a valid object to be written out as .mat file, but I'd say its structure is similar to my original data file.
x <- list(baskets = list(a = c(7, 8), b = c(2, 3)), fruits = c("apple", "orrange"))
rmatio::write.mat(x, "list-name-ex.mat")
x_rmatlab <- R.matlab::readMat("list-name-ex.mat")
x_rmatio <- rmatio::read.mat("list-name-ex.mat")
x_rmatlab$baskets
#> , , 1
#>
#> [,1]
#> a numeric,2
#> b numeric,2
x_rmatlab$baskets[[1]]
#> [,1] [,2]
#> [1,] 7 8
x_rmatlab$baskets[[2]]
#> [,1] [,2]
#> [1,] 2 3
x_rmatlab$baskets[["a"]]
#> NULL
x_rmatlab$baskets[["b"]]
#> NULL
x_rmatio$baskets
#> $a
#> $a[[1]]
#> [1] 7 8
#>
#>
#> $b
#> $b[[1]]
#> [1] 2 3
x_rmatio$baskets[["a"]]
#> [[1]]
#> [1] 7 8
x_rmatio$baskets[["b"]]
#> [[1]]
#> [1] 2 3
Created on 2022-06-12 by the reprex package (v2.0.0)
As you can see, R.matlab::readMat() knows the first level of names (baskets and fruits) but not a and b within baskets. Note: I tried R.matlab::writeMat() as well, but for some reason it doesn't write out the nested list.