MATLAB: How can thousands of matrix lines be written into a multidimensional cell-array

cellcell arrayscreate cell arrayfor loopMATLAB and Simulink Student Suitematrix array

I have created a cell array as follows:
YY = cell(31,10);
for l = 1:numel(YY)
YY{l} = zeros(1000,3);
end
YY has now 31 rows and 10 columns and each YY{i,j} has now 1000 rows and 3 columns.
The goal is now to fill the individual cells, with the elements of a matrix (which has some thousand lines and three columns).
I have tried the following to write the individual rows of the matrix into the respective cell:
% reading of simulated runoff (simulated)
S = 'runoff.txt'; % name of file (always the same)
D = 'C:\Users\heute\model\model_standalone\'; %pathname
d = dir(fullfile(D,'results*')); % file name is changing with consecuitive number
fmtS = ['%s',repmat('%f',1,6)]; % Format
opt = {'HeaderLines',1,'CollectOutput',true}; % headerline
L=length(d); % number of sub-folders
C=zeros(L,3); % preallocate
YY = cell(31,10); %create cell array
for l = 1:numel(YY) % create cells with a dimension of (1000,3)
YY{l} = zeros(1000,3);
end
for k = 1:L % loop to open files, make calculations and fill the cell arrays
fid = fopen(fullfile(D,d(k).name,S),'rt'); % open the files with simulated data
Z=textscan(fid,fmtS,opt{:}); % read simulated data
fclose(fid); % close file
dtS=Z{:,1}; % timestamp simulated (datetime)
Qs=Z{:,2}(:,6); % variable with simulated runoff
% Define timesteps, which are used for analysis
yr1=2014; yr2=2016;
ix=iswithin(dto,datenum(yr1,05,01),datenum(yr1,10,01)); % first year
for yr=yr1+1:yr2 % subsequent years
ix=ix | iswithin(dto,datenum(yr,05,01),datenum(yr,10,01));
end
f_1k = 1-cov(Qs(ix)-Qo(ix))/var(Qo(ix)); % NSE
f_2k = rms(Qs(ix)-Qo(ix)); % RMSE
f_3k = abs(mean(Qs(ix)-Qo(ix))); % BIAS
C(k,:) = [f_1k, f_2k, f_3k];
% write matrix C into the cell array
YY{k} = C;
end
With this variant, all individual cells are filled with the same values.
Another variant with a for – loop writes only one row in each cell. In this case four cells are filled with only one row.
for ii=1:4
YY(ii) = {C(ii, :)};
end
How can you solve the problem, that the first 1000 values are written in the first cell, the second 1000 values in the second cell etc?

Best Answer

Write them into a large numeric array, possibly a 4 dimension one. mat2cell() afterwards.