MATLAB: What’s wrong on the script that gives me Index exceeds matrix dimensions error??????

index exceeding

%Creating the regressor for the GLM from the coefficent of correlation
%resulted from the fitting process.
fs = 250; %frequency sample rate
NTR = 400; % number of trigs resulted by Scan Star
TR= 750;
%trigs(end+1)=trigs(end)+round(mean(diff(trigs)));
trigs=TR:TR*fs:NTR*TR*fs;
fit_corr = (data(:,5)).^2; % taking the 5th column of the file (correlation) and squared it to have the coefficent of determination
% fit_corr = data(:,5);
% mean_r2=mean(fit_corr(trigs(1):trigs(end))) %for each TR, take the mean value
hrf= spm_hrf(1/fs); %convolution of the vector resulted from above
% hrf=hrf/std(hrf);
% td = diff(hrf);
% td=td/std(td);
% dd = diff(td);
% dd=dd/std(dd);
reg = conv(fit_corr,hrf);
% reg_td = conv(fit_corr,td);
% reg_dd = conv(fit_corr,dd);
reg_fit = zeros(NTR,1);
% reg_fit_td = zeros(NTR,1);
% reg_fit_dd = zeros(NTR,1);
%sign=mean(reg(10302:750:309648));
for i=1:NTR
regr_fit(i)=mean(reg(trigs(i):trigs(i)));
% reg_fit_td(i)=mean(reg_td(trigs(i):trigs(i+1)-1));
% reg_fit_dd(i)=mean(reg_dd(trigs(i):trigs(i+1)-1));
end
figure
plot(regr_fit)
% plot (sign)

Best Answer

We cannot run your script, because we do not know "data". In addition you forgot to mention, which line is failing with the error message.
Therefore I suggest to use the debugger:
dbstop if error
Then Matlab stops when the error occurs and you can check the sizes of all variables. The you will find out, that you try to access a not existing element of an array as in "x = 1:4, disp(x(5))".
You can search in this forum to find out, that there are dozens of equivalent questions with the same answer.
Related Question