How to read multiple files into a single cell arra

2019-06-14 06:32发布

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!

2条回答
叛逆
2楼-- · 2019-06-14 07:17

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

查看更多
迷人小祖宗
3楼-- · 2019-06-14 07:33

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];
查看更多
登录 后发表回答