Help with 'Index out of bounds error' to makeExelTable
2 ビュー (過去 30 日間)
古いコメントを表示
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)
load(currentFiles{f})
currentFiles{f}
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;
end
if ~isnan(tempBandwidth(1))
error('Warning to check if TC needs to be flipped')
end
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;
end
end
end
end
save('ExcelData.mat', 'excelStringData', 'excelNumericalData', 'code');
1 件のコメント
John Chilleri
2017 年 3 月 1 日
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.
回答 (1 件)
Nagarjuna Manchineni
2017 年 3 月 7 日
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)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!