如何在 matlab 中将嵌套列表转换为结构(.mat)

how to convert nest list into structure(.mat) in matlab

我发现rmatio是一个很好的工具,可以将数据框和列表转换成.mat文件(虽然似乎不支持该字符),但在下面的演示中,我想生成一个嵌套列表,名为data_list,但是当我用write.mat导出为.mat文件时,只保留了第一列数据,请问如何解决这样的问题?提前致谢。

library(rmatio)  
data(iris)
iris
iris$group<-paste("group",rep(1:2,75),sep="")

names(iris)<-c('a','b','c','d','species','group')
## "."is not support as name in .mat file, so I rename it as "a b c d" for simplicity.
head(iris)
##split the data into list according to species.
out<-split(iris[,c(1:4,6)],f=iris$species)
out
##further split the data by group variable to create a nest list.
data_list<-lapply(out,function(x){
split(x,f=x$group)
})

write.mat(data_list, 'iris.mat')

在安装和加载 Octave 并学习了几个命令并试验了 write.mat 函数之后,我得出结论,它不处理嵌套列表。它显然将列表中的每个项目写入 matlab 文件中的单独变量。因此,如果您希望 iris 中的所有数据(分成两组)进入一个 matlab 文件,并以用 rmatio 编写的单独变量结束,您需要这样的东西:

iris$group <- 1:2
data_list <- 
split(unlist(iris[1:4]), f=interaction(iris$Species, iris$group, rep(names(iris)[1:4], each=150)))
str(data_list)
write.mat(data_list, 'iris2.mat')

然后在 Octave 会话中您会看到:

octave-3.4.0:19> load /Users/davidwinsemius/iris2.mat 
octave-3.4.0:20> whos
Variables in the current scope:

  Attr Name                           Size                     Bytes  Class
  ==== ====                           ====                     =====  ===== 
       setosa.1.Petal.Length          1x25                       200  double
       setosa.1.Petal.Width           1x25                       200  double
       setosa.1.Sepal.Length          1x25                       200  double
       setosa.1.Sepal.Width           1x25                       200  double
       setosa.2.Petal.Length          1x25                       200  double
       setosa.2.Petal.Width           1x25                       200  double
       setosa.2.Sepal.Length          1x25                       200  double
       setosa.2.Sepal.Width           1x25                       200  double
       versicolor.1.Petal.Length      1x25                       200  double
       versicolor.1.Petal.Width       1x25                       200  double
       versicolor.1.Sepal.Length      1x25                       200  double
       versicolor.1.Sepal.Width       1x25                       200  double
       versicolor.2.Petal.Length      1x25                       200  double
       versicolor.2.Petal.Width       1x25                       200  double
       versicolor.2.Sepal.Length      1x25                       200  double
       versicolor.2.Sepal.Width       1x25                       200  double
       virginica.1.Petal.Length       1x25                       200  double
       virginica.1.Petal.Width        1x25                       200  double
       virginica.1.Sepal.Length       1x25                       200  double
       virginica.1.Sepal.Width        1x25                       200  double
       virginica.2.Petal.Length       1x25                       200  double
       virginica.2.Petal.Width        1x25                       200  double
       virginica.2.Sepal.Length       1x25                       200  double
       virginica.2.Sepal.Width        1x25                       200  double

Total is 600 elements using 4800 bytes

有一个 R.matlab-package 用较少的 Baroque 过程完成了虹膜数据帧的传输:

> require(R.matlab)
Loading required package: R.matlab
R.matlab v3.2.0 (2015-02-24) successfully loaded. See ?R.matlab for help.

Attaching package: ‘R.matlab’

The following objects are masked from ‘package:base’:

    getOption, isOpen

> writeMat("iris3.mat", iris=iris, matVersion="5", onWrite=NULL, verbose=FALSE)

传输带有 6 个名为 columns.It 的单个对象也能够将 data_list 结构作为具有 24 列的单个项目传输。

octave-3.4.0:35> load /Users/davidwinsemius/dl.mat 
octave-3.4.0:36> whos
Variables in the current scope:

  Attr Name           Size                     Bytes  Class
  ==== ====           ====                     =====  ===== 
       data_list      1x1                       4800  struct
       iris           1x1                       4800  struct

Total is 2 elements using 9600 bytes