MATLAB Answers


Help with 'Index out of bounds error' to makeExelTable

Asked by Katayoun Sayat on 1 Mar 2017
Latest activity Edited by Walter Roberson
on 7 Mar 2017
myData.GroupNames = {..};
myData.GroupFiles{1} = {..};
myData.GroupFiles{2} = {..};
excelStringData = {};
excelNumericalData = [];
code = {'Group', 'RatID', 'Field', 'CF', 'Threshold', 'BW10', 'BW20', 'Lat1', 'Lat2', 'Lat3'};
count = 1;
for g = 1:length(myData.GroupNames)
currentFiles = myData.GroupFiles{g};
for f = 1:length(currentFiles)
for i = 1:length(data)
if data{i,1}.Field ~= 0 && data{i,1}.Field < 5
tempThreshold = data{i,1}.Threshold;
tempBandwidth = data{i,1}.Bandwidth';
%Fix for some threshold rounding errors
if mod(tempThreshold,10) ~= 0
tempThreshold = tempThreshold*10;
if ~isnan(tempBandwidth(1))
error('Warning to check if TC needs to be flipped')
tempBW20 = tempBandwidth(tempThreshold/10+3);
tempBW10 = tempBandwidth(tempThreshold/10+2);
excelNumericalData(count,1) = data{i,1}.Field;
excelNumericalData(count,2) = data{i,1}.CF;
excelNumericalData(count,3) = tempThreshold;
excelNumericalData(count,4) = tempBW10;
excelNumericalData(count,5) = tempBW20;
excelNumericalData(count,6:8) = data{i,1}.Latency;
excelStringData{count,1} = myData.GroupNames{g};
excelStringData{count,2} = strcat([myData.GroupNames{g} '_' num2str(f)]);
count = count + 1;
save('ExcelData.mat', 'excelStringData', 'excelNumericalData', 'code');

  1 Comment

My guess is that your tempThreshold variable is multiplied by 10 multiple times, but when you use it as an index, you only divide by 10 once. This seems like it could easily cause a problem.

Sign in to comment.

1 Answer

Answer by Nagarjuna Manchineni on 7 Mar 2017

Looking at your code there might be multiple possible scenarios that could cause "Index exceeding Matrix dimensions":
1) length(data) will return the maximum of (num rows, num columns). For example,
>> length({1 2 3;4 5 6}) % is 3
>> length({1 2 3;4 5 6;7 8 9; 10 11 12}) % is 4.
You are trying to access some struct value in the data variable using data{i,1}, which might have caused the issue. For example, when data variable is of size 3x1 there will be no problem and if it is of size 1x3 the issue will arise.
Instead of using length(data) use
>> dataSize = size(data);dataSize(1) %for number of rows
2) An edge case when tempBandwidth is an empty array/matrix. For example,
>> tempBandwidth = []; % tempBandwidth(1) gives the Index exceeds Matrix dimensions issue
To verify if a variable is not empty, you can use the 'isempty' function as shown below:
>> isempty(tempBandwidth)
3) The following line in your MATLAB script can cause ""Subscript indices must either be real positive integers or logicals." error:
tempBW20 = tempBandwidth(tempThreshold/10+3); % as the index can be a decimal number if tempThreshhold is not a number of 10 (I see that you are already multiplying tempThreshhold by 10 for rounding issues)
To prevent that error from occuring, also as a best practice use round or floor or ceil depends on the use case.


Sign in to comment.