It's a little difficult to visualize what this looks like, but here's what i took away from looking at your code
- you have a thing called 'count' for each iteration of your loop
- count is the result of calling hist
- you want to plot both the average (across iterations) of count, and each individual count.
I think you're storing your counts in one big vector, so you have a count for each bin in your histogram, but all in a big vector. You could probably use reshape to get this into the shape you want for plotting. What I mean by that is you currently have
[ participant1bin1, participant1bin2, participant1bin3, ... participant2bin1, participant2bin2, ....]
and you can reshape to
[ participant1bin1, participant1bin2, participant1bin3, ...
participant2bin1, participant2bin2, ....]
But I wouldn't take this approach, instead it makes more sense to just store the data in the correct order. In general, instead of appending to an array, just specify where you should go in the loop. Here's an example where I bin some random normal data:
binned_data = nan(height(data), length(bincenters));
binned_data(i,:) = hist(data(i,:), bincenters);
Now, binned_data has a row for each histogram, and a column for each bin. By default, plot will make a series for each column rather than each row, but if we specify an x, it knows what to do.
plot(bincenters, binned_data)
Now let's calculate the mean and put it on top of there:
plot(bincenters, mu, 'k-','linewidth',1.5)
To summarize: you're getting into trouble because you have a big long row vector and MATLAB doesn't know where one series ends and the next starts. You could probably use reshape to turn that vector into a matrix, but the better approach is to just store it as a matrix in the first place. Initialize the matrix before the loop and then store each row in the matrix rather than 'appending'.
Some other tips on looking at your code:
- I recommend histcounts over hist
- instead of using cd, it's better to specify the full path to the filenames