将数组从matlab导入R

时间:2022-02-12 15:26:27

I'm sure this is a simple problem, but I haven't been able to find an obvious solution. I have a series of model output array files (dim 180, 360, 12) generated in matlab that I need to open in R.

我确信这是一个简单的问题,但我找不到明显的解决方案。我在matlab中生成了一系列模型输出数组文件(dim 180,360,12),我需要在R中打开它。

I have attempted to use the R.matlab package, simply using the readMat command, and this results in a list object. Attempting to write this list into a matrix results in a memory allocation error.

我试图使用R.matlab包,只需使用readMat命令,这就产生了一个列表对象。尝试将此列表写入矩阵会导致内存分配错误。

I have tried unlisting, but that did not help either.

我尝试过不公开,但这也没有帮助。

How can I open these matlab matrix files and write as a matrix in R? Any thoughts?

如何打开这些matlab矩阵文件并在R中写为矩阵?有什么想法吗?


code for the matlab read so far is simply:

到目前为止,matlab读取的代码很简单:

> data<-readMat("filename")
> typeof(data)
[1] "list"
> str(data)
List of 1
 $ pco2: num [1:180, 1:360, 1:12] NaN NaN NaN NaN NaN ...
 - attr(*, "header")=List of 3
 ..$ description: chr "MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Thu Jul 26 10:36:42 2012 "
 ..$ version : chr "5"
 ..$ endian : chr "little

1 个解决方案

#1


15  

Please double check your file inside MATLAB, and make sure not all values are NaN. Simply load the file in MATLAB and inspect the contents:

请仔细检查MATLAB中的文件,并确保并非所有值都是NaN。只需在MATLAB中加载文件并检查内容:

load file.mat

Here is a simple test I just did:

这是我刚刚做的一个简单的测试:

MATLAB

>> x = rand(3,4,2)

x(:,:,1) =
      0.75127      0.69908      0.54722      0.25751
       0.2551       0.8909      0.13862      0.84072
      0.50596      0.95929      0.14929      0.25428
x(:,:,2) =
      0.81428      0.34998      0.61604      0.83083
      0.24352       0.1966      0.47329      0.58526
      0.92926      0.25108      0.35166      0.54972

>> save file.mat x

R

R> library(R.matlab)
R> data <- readMat('file.mat')
R> str(data)
List of 1
 $ x: num [1:3, 1:4, 1:2] 0.751 0.255 0.506 0.699 0.891 ...
 - attr(*, "header")=List of 3
  ..$ description: chr "MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: ..."
  ..$ version    : chr "5"
  ..$ endian     : chr "little"
R> data$x
, , 1

       [,1]   [,2]   [,3]   [,4]
[1,] 0.7513 0.6991 0.5472 0.2575
[2,] 0.2551 0.8909 0.1386 0.8407
[3,] 0.5060 0.9593 0.1493 0.2543

, , 2

       [,1]   [,2]   [,3]   [,4]
[1,] 0.8143 0.3500 0.6160 0.8308
[2,] 0.2435 0.1966 0.4733 0.5853
[3,] 0.9293 0.2511 0.3517 0.5497

#1


15  

Please double check your file inside MATLAB, and make sure not all values are NaN. Simply load the file in MATLAB and inspect the contents:

请仔细检查MATLAB中的文件,并确保并非所有值都是NaN。只需在MATLAB中加载文件并检查内容:

load file.mat

Here is a simple test I just did:

这是我刚刚做的一个简单的测试:

MATLAB

>> x = rand(3,4,2)

x(:,:,1) =
      0.75127      0.69908      0.54722      0.25751
       0.2551       0.8909      0.13862      0.84072
      0.50596      0.95929      0.14929      0.25428
x(:,:,2) =
      0.81428      0.34998      0.61604      0.83083
      0.24352       0.1966      0.47329      0.58526
      0.92926      0.25108      0.35166      0.54972

>> save file.mat x

R

R> library(R.matlab)
R> data <- readMat('file.mat')
R> str(data)
List of 1
 $ x: num [1:3, 1:4, 1:2] 0.751 0.255 0.506 0.699 0.891 ...
 - attr(*, "header")=List of 3
  ..$ description: chr "MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: ..."
  ..$ version    : chr "5"
  ..$ endian     : chr "little"
R> data$x
, , 1

       [,1]   [,2]   [,3]   [,4]
[1,] 0.7513 0.6991 0.5472 0.2575
[2,] 0.2551 0.8909 0.1386 0.8407
[3,] 0.5060 0.9593 0.1493 0.2543

, , 2

       [,1]   [,2]   [,3]   [,4]
[1,] 0.8143 0.3500 0.6160 0.8308
[2,] 0.2435 0.1966 0.4733 0.5853
[3,] 0.9293 0.2511 0.3517 0.5497