Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get R data into a matlab matrix

Tags:

r

matrix

matlab

I have a large data matrix in R. I used the package 'R.matlab' to convert the data to matlab data like so:

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 I have a 1x1 struct.

I would like to get this into a matrix form. I tried:

data=struct2cell(x) 

but that doesn't look quite right. The data is decimal valued numbers btw.

like image 399
bdeonovic Avatar asked Mar 04 '13 16:03

bdeonovic


2 Answers

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

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

Another way you may want is to convert the cell array to matrix in MATLAB:

datanum = cell2mat(data');
like image 143
yuk Avatar answered Nov 16 '22 04:11

yuk


If you are using Jupyter notebook. I think what you have to do is first install R.matlab library using

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

Then implement this library using

library(R.matlab)

Afterward, you may have a dataframe in R say resi

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

I now go to Matlab and call it in that particular directory by the converting the struct to cell and then cell to matrix as

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

I hope that helps

like image 39
rsc05 Avatar answered Nov 16 '22 03:11

rsc05