Using R to write a .mat file not giving the right output?

回眸只為那壹抹淺笑 提交于 2019-12-24 04:30:17

问题


I had a .csv file that I wanted to read into Octave (originally tried to use csvread). It was taking too long, so I tried to use R to workaround: How to read large matrix from a csv efficiently in Octave This is what I did in R:

forest_test=read.csv('forest_test.csv')
library(R.matlab)
writeMat("forest_test.mat", forest_test_data=forest_test)

and then I went back to Octave and did this:

forest_test = load('forest_test.mat')

This is not giving me a matrix, but a struct. What am I doing wrong?


回答1:


To answer your exact question, you are using the load function wrong. You must not assign it's output to a variable if you just want the variables on the file to be inserted in the workspace. From Octave's load help text:

If invoked with a single output argument, Octave returns data instead of inserting variables in the symbol table. If the data file contains only numbers (TAB- or space-delimited columns), a matrix of values is returned. Otherwise, 'load' returns a structure with members corresponding to the names of the variables in the file.

With examples, following our case:

## inserts all variables in the file in the workspace
load ("forest_test.mat");
## each variable in the file becomes a field in the forest_test struct
forest_test = load ("forest_test.mat");

But still, the link you posted about Octave being slow with CSV files makes referece to Octave 3.2.4 which is a quite old version. Have you confirmed this is still the case in a recent version (last release was 3.8.2).




回答2:


There is a function designed to convert dataframes to matrices:

?data.matrix

forest_test=data.matrix( read.csv('forest_test.csv') )
library(R.matlab)
writeMat("forest_test.mat", forest_test_data=forest_test)


来源:https://stackoverflow.com/questions/25346314/using-r-to-write-a-mat-file-not-giving-the-right-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!