How to read multiple files into a single cell array?

a 夏天 提交于 2020-01-14 05:44:26

问题


I have a large dataset split into 5 files (each has 15000 attributes, first file contains header (attribute names) and 9999 records, and the other 4 contain 10000 records).

Using textscan, I have created 5 cell arrays which have to be merged and don't know whether this approach is appropriate or it would be better to directly read all 5 files into single cell array. Anyway I would be thankful if anyone of you could show the way to merge several cell arrays into single cell array or read several text files into single cell array.

Thank you!


回答1:


Unless you want to do some Java magic, you cannot read multiple files into a single array directly.

However, once you have obtained the cell arrays, it should be easy to combine them: Assuming that there are the same number of columns in each cell array, you can concatenate them like this:

finalCell = [cell1;cell2;cell3;cell4;cell5];



回答2:


Expanding on Jonas' answer, If memory is a concern, you could combine them as you read the files to avoid having 5 x 15000 x 10000 + 1 15000 x 50000 cell arrays.

finalCell = textscan(fid_1,'format');

finalCell = [finalCell; textscan(fid_2,'format')];

finalCell = [finalCell; textscan(fid_3,'format')];

finalCell = [finalCell; textscan(fid_4,'format')];

finalCell = [finalCell; textscan(fid_5,'format')];

Best Regards,

Adam



来源:https://stackoverflow.com/questions/3459592/how-to-read-multiple-files-into-a-single-cell-array

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