0

I have a structure in matlab for example

A =

in: [200x1 double]

cols: {28x1}

rows: [200x28 double]

How do I create a data frame in R which has A.cols as the columns and A.rows as the header and A.in as the data

I am using R.Matlab package but I get stuck when I read the mat file in, how do I do this? Thanks for your help!

qfd
  • 778
  • 2
  • 10
  • 24

1 Answers1

1

In MATLAB: convert your A to a dataset and use export() function to create a text file (in stead of .mat file). Note that your column and row names must be cells of strings but not numeric vectors. As your A.in is a numeric matrix:

A.rows = [11,12,13,14,15];      %#  numeric vector
A.cols = {'A','B','C','D','E'}; %#  cell of strings
A.in = magic(5);                %#  numeric matrix
DS = mat2dataset(A.in,'VarNames',A.cols, 'ObsNames',cellstr(num2str(A.rows')));
export(DS,'file','A.txt')         

Name for your file as you desire, e.g. 'A.txt'. You may check the contents of your file, called "A.txt" by:

 type A.txt

In R choose the folder with your 'A.txt' file and use function read.table():

A <- read.table('A.txt',header = TRUE)
rownames(A)    <- A$Observations
A$Observations <- NULL

Explore your desired MATLAB variable in R:

head(A)
#     A  B  C  D  E
# 11 17 24  1  8 15
# 12 23  5  7 14 16
# 13  4  6 13 20 22
# 14 10 12 19 21  3
# 15 11 18 25  2  9

Please note that in the example you provided, your data is in A.rows, and row names are in A.in.

GegznaV
  • 4,938
  • 4
  • 23
  • 43