I've reformatted the code in your question to help myself understand it.
function [D]=AH(nr,AAG,s)
for y=1:nr
xx=cell(length(AAG{y}),1);
for o=1:length(AAG{y})
xx{o} =[AAG{y}{o} zeros(1,s-length(AAG{y}{o}))];
end
for k1 = 1:length(AAG{y})
% %Histogram over all values
if k1==1
G(k1:length(xx{k1}))=xx{k1};
else
G((k1-1)*s+1:k1*s)=xx{k1};
% G=[G xx{k1}];
end
%Plotting inhabitants
% scatter(ones(1,numel(xx{k1}))*y, xx{k1},ones(1,numel(xx{k1}))*I{k1},'s','Linewidth',10)
end
if y==1
D1=G.';
end
% and continuing on like that for y==2 to y == 36 and D2 to D36
end
D=[D1;D2;D3;D4;D5;D6...]; % etc
end
This function is much more complicated than it has to be. For example:
function D = AH(nr,AAG,s)
D = [];
for y = 1:nr
data_length = length(AAG{y})
for o = 1:data_length % you can wrap this and the next for loop together
G((o-1)*s+1:o*s) = [AAG{y}{o} zeros(1,s-length(AAG{y}{o}))].';
% I'm not sure why you needed the additional condition here
% since as far as I can tell, G((1-1)*s+1:1*s) evaluates the
% same as G(1:length(xx{k1}))
% Additionally, now G is already flipped.
end
D = [D; G]; % Now, the G from 1:nr is automatically appended to the end of D.
end
end
Finally, the error is because I3 and D end up different sizes, so there might actually be a problem in your "Inhabitants" function or in this one. Without more information (such as the final sizes of D and I3 when the function works and when it does not, and the Inhabitants function) the best advice I can really give is to write out the logic of all your subfunctions to determine exactly what happens to the data (size and transformations) and figure out the difference between what you intend the program to be doing and what you have told the program to do.
I would also recommend you change your variable names, because it's almost impossible to follow what the program is doing. It seems like you're trying to keep the purpose of your program obscured, but it doesn't add any insight to your program if for example:
function dependent_variable = reformat_function(nr,raw_data,expected_length)
dependent_variable = [];
for y = 1:nr
personal_data = [];
data_length = length(raw_data{y})
for o = 1:data_length
personal_data((o-1)*expected_length+1:o*expected_length) = ...
[raw_data{y}{o} zeros(1,s-length(raw_data{y}{o}))].';
end
dependent_variable = [dependent_variable; personal_data];
end
end
Overall, it seems like you're trying to reorder the data from a multilevel cell array; it does feel like there should be a simpler way to do this, though. For example, if AAI{j}{k} corresponds to AAG{j}{k}, you could reformat this for a series of plots simply enough:
figure()
hold on
for ind_1 = 1:nr
plot_x = [];
plot_y = [];
for ind_2 = 1:length(AAI{ind_1})
plot_x = [plot_x AAI{ind_1}{ind_2}];
plot_y = [plot_y AAG{ind_1}{ind_2}];
end
plot(plot_x,plot_y,'.')
end