MATLAB Answers

how to convert ,mat data file to R

445 views (last 30 days)
JFz
JFz on 26 Feb 2019
Commented: Trey Brown on 9 Jun 2019
Hi,
I recently got a task to convert a .mat data file into an R datafile.
I don't know how about R at all. So I am asking any leads on how to convert matlab .mat file into R based data file?
Thanks!

  0 Comments

Sign in to comment.

Accepted Answer

Asad Mirza
Asad Mirza on 28 Feb 2019
Edited: Asad Mirza on 28 Feb 2019
As far as I know R can read MATLAB data using read.mat. So taking the example from here.
>> 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> 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

  3 Comments

JFz
JFz on 28 Feb 2019
Thank you!
I tried it and there is an error message saying I have to convert my .mat file into v6.
After I converted it to v6, I can use read to load in the mat file.
Is there any function in R that can read any version of .mat file?
Thanks again.
Trey Brown
Trey Brown on 9 Jun 2019
how did you convert it to the v6?
Trey Brown
Trey Brown on 9 Jun 2019
Nevermind, my answer was found using the link below in case anyone else runs into it..

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2018b

Translated by