I have 812 text files in one folder and 649 text files in another folder(these text files are image descriptors), and each of text files contains about 3000 numbers with this pattern: first 5 numbers are location of descriptors and 128 next numbers are values that I want to save them as a column in cell array, and this pattern repeats till the end of text file. and my goal is extracting all descriptors in a 128*n cell array, in which n is the number of descriptors for all images. here is my code for extracting all descriptors of all text files in one cell array
function cel = affinedesc(fname)
FID = fopen(fname, 'r');
content = textscan(FID, '%s');
content = content{1,1};
cel = cell(1,str2num(content{2,1}));
content = content(3:end);
fclose(FID);
counter = 1;
for i=1:133:length(content)-1
t1 = i+5;
t2 = i+4+128;
cel{counter} = content(t1:t2);
counter = counter+1;
end
cel = cat(2,cel{:});
end
function descscel = affinedescs(dir)
desccel = {};
for i=1:length(dir)
fname = dir(i).name;
cel = affinedesc(fname);
desccel{i} = cel;
end
descscel = cat(2,desccel{:});%here my pc freezes!
end
now here is my question: it works correct but the final cell doesn't appear in matlab workspace and I can't save the final cell for all text files that it is concatenation of all cell of all text files, and my PC screen freezes. I think it's because my final cell array is too LARGE, I wanted to know if there is a better way?
any help is appreciated!
Can you verify that your computer memory runs out? (For example via Task Manager in Windows.)
If it is a memory problem, try avoiding dynamically growing your arrays/cells inside the loops. Pre-allocate memory by defining a null-variable of the correct size. The cell array does not require contiguous memory but each cell does. Read more here
Also, there's a typo in the line that freezes your computer. Is the
descel
variable created elsewhere?I'm aware that this might not qualify as an answer, but I haven't got enough reputation to post comments.