matlab到R的导入结构 [英] matlab to R import structure

查看:167
本文介绍了matlab到R的导入结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我在matlab中有一个结构

I have a structure in matlab for example

A =

输入:[200x1两倍]

in: [200x1 double]

cols:{28x1}

cols: {28x1}

行:[200x28 double]

rows: [200x28 double]

如何在R中创建一个以A.cols作为列和A.rows作为标题的数据框 和A.in作为数据

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

我正在使用R.Matlab软件包,但是当我读入Mat文件时被卡住了,我该怎么办? 感谢您的帮助!

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!

推荐答案

MATLAB中:A转换为数据集,并使用export()函数创建文本文件(而不是.mat文件).请注意,列名和行名必须是字符串的单元格,而不是数字矢量. 由于您的A.in是数字矩阵,所以:

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')         

根据需要为文件命名,例如"A.txt". 您可以通过以下方式检查名为"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

R 中,选择包含'A.txt'文件的文件夹,然后使用功能read.table():

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

在R中浏览所需的MATLAB变量:

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

请注意,在您提供的示例中,数据位于A.rows中,行名位于A.in中.

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

这篇关于matlab到R的导入结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆