How to get R data into a Matlab matrix

I have a large data matrix in R. I used the package 'R.matlab' to convert the data into Matlab data as follows:

 writeMat(con="...filepath", x=data) 

I have no experience with Matlab, so please be patient with me:

When I load the data into matlab, it says that I have a 1x1 structure.

I would like to get this in matrix form. I tried:

 data=struct2cell(x) 

but it doesn’t look quite right. Data is decimal significant number of bits.

+6
source share
2 answers

It looks like your data variable in R is a data frame. Try first converting it to a matrix before writing to the mat file:

 writeMat(con="...filepath", x=as.matrix(data)) 

Another way you might want to convert a cell array into a matrix in MATLAB:

 datanum = cell2mat(data'); 
+6
source

If you are using a Jupyter laptop. I think you need to install the R.matlab library first using

 install.packages(c('R.matlab'), repos='http://cran.us.r-project.org') 

Then we implement this library with

 library(R.matlab) 

After that, you may have a data frame in R say resi

 #Save in Matlab v6 format with 'writeMat' writeMat("resi.mat", labpcexport = resi) 

Now I go to Matlab and call it in this particular directory by converting the structure into a cell, and then the cells into a matrix as

 resi=cell2mat(struct2cell(load('resi.mat'))) 

I hope this helps

+1
source

All Articles