Export cell array to multi-column csv file

て烟熏妆下的殇ゞ 提交于 2019-12-10 11:44:37

问题


Here is another trivial question on octave that hopefully somebody can assist me with. I have a script in octave which generates a cell array. I now want to go about exporting this as some form of text file that I can import into R for plotting and further statistical analysis (csv probably makes the most sense as a format). I have read the section entitled "14.1.3 Simple File I/O" as well as the more specific section on the writecsv function however it is not obvious to me how you might export a cell array. Here is an example dataset in the same form as mine:

test = {[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]};

and this is what test.csv would look like:

1,5,9
2,6,10
3,7,11
4,8,12

Thanks for any advice.


回答1:


The following command should work for your test case:

csvwrite ("data.csv", cell2mat (test')')

To understand how it works, run the following on your Octave session:

test = {[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]};
cell2mat (test)
test' 
cell2mat (test')
cell2mat (test')'
cell2mat (test)'


来源:https://stackoverflow.com/questions/14378996/export-cell-array-to-multi-column-csv-file

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