MATLAB: Cell to mat-file

cell array

Hi
As far as i know partial loading with MAT-file does not support a variable of cell class. My question is if there exists a some way to partial load a cell of size 1x n

Best Answer

Mathworks specifically documents that you cannot do a partial load of a cell array.
You would have to break up your cell array into variables when you stored it in the matfile. For example,
chunksize = 10;
varbase = 'MyVar';
chunkname = cat(varbase, '_chunk_');
m = matfile('myFile.mat','Writable',true);
varlen = size(MyVar, 1); %I am going to presume chunking by groups of rows
chunklist = [1 : chunksize : varlen, varlen+1];
numchunks = length(chunklist) - 1;
chunknames = cell(numchunks,1);
for chunknum = 1 : numchunks
thisvarstr = sprintf('%s%d', chunkname, chunknum);
m.(thisvarstr) = MyVar(chunklist(chunknum):chunklist(chunknum+1)-1,:);
chunknames{chunknum} = thisvarstr;
end
chunkinfo = struct('chunklist', chunklist, 'chunksize', chunksize, 'size', size(MyVar), 'chunknames', chunknames, 'chunkname', chunkname);
chunkinfostr = cat(chunkname, 'info');
m.(chunkinfostr) = chunkinfo;
This produces file variables MyVar_chunk_info with some descriptive information, and MyVar_chunk_1 MyVar_chunk_2 and so on to hold the information.
Given any particular column index, the chunk number can be computed based upon chunksize. Or you could use the chunk list, which is structured as a list of boundaries such as [1 11 21 22] with the N'th chunk starting at the cell index given in the N'th entry and ending before the cell index given in the next entry -- so the above list would describe chunks with indices {1:10, 11:20, 21}.
[counts, chunknumber] = histc(rowindexlist, chunklist);
Adapt for whatever flexibility you need.