MATLAB: Running out of memory in a not so big matrix

out of memory

i wrote a program to extract data from various files and for some reason ~10% of them give me a "Maximum variable size allowed by the program is exceeded" error. The matrices in the ones that don't work aren't bigger than the ones that do work (less than a 500×10000 matrix). The code basically looks like this:
tempsave=zeros(c.trials,9900);
accumulate=0;
for j=1:c.trials %e.g. 398 trials
spikes_in_trial=c.spike_end(j)-c.spike_start(j)+1; %find spikes in this trial
accumulate=spikes_in_trial+accumulate; %total spikes for cell
for k=accumulate-spikes_in_trial+1:accumulate
tempsave(j,c.spikes(k))=1000; %1000 to convert to Hz %error occurs here at j=241 , k=24023
end
end
%where in this example c.trials is [398], c.spike_start is [398×1], c.spike_end is [398×1] and c.spikes is [42004×1]
Even when I try and divide this into 2 separate for loops (for j=1:c.trials/2) I get the same error at the exact same point (trial 241).
I tried all types of solutions that worked for other people to no avail. Any ideas?
Oh and for what it's worth my memory stats: Maximum possible array: 1176 MB (1.233e+009 bytes) Memory available for all arrays: 2550 MB (2.674e+009 bytes) * Memory used by MATLAB: 276 MB (2.899e+008 bytes) Physical Memory (RAM): 3510 MB (3.681e+009 bytes)

Best Answer

I suggest you breakpoint your code (using conditional breakpointing) just before the line that fails, at j==241, k==24023. Then look at the values and sizes of your variables.
What is the value of c.spikes(24023), which is being used as an index? My best guess is that that value is large (e.g. much larger than 10,000), which is why you are getting that specific error.