MATLAB: Index in position 1 is invalid. Array indices must be positive integers or logical values.

index

I am having a problem with my index variable, and I don't know why the issue is happening. I am using a time found from one variable (LHSTime) to calculate the index where the associated data is found in another variable. The code is seen below.
for n=1:12
UDFES01_HeelMarkers{n,1}=readmatrix("UDFES01_HeelMarkers","Sheet",sheets(n));
UDFES01_LON{n,1}=readmatrix("UDFES01_Events","Sheet",sheets(n),"Range","B:B");
if isempty(UDFES01_LON{n,1})
continue;
end
UDFES01_RON{n,1}=readmatrix("UDFES01_Events","Sheet",sheets(n),"Range","E:E");
for ii=1:length(UDFES01_LON{n,1})
LHSTime=UDFES01_LON{n,1}(ii,1);
if isnan(LHSTime)
break;
end
index=(LHSTime*sampRate)+1;
UDFES01_LStepLength(ii,n)=UDFES01_HeelMarkers{n,1}(index,2)-UDFES01_HeelMarkers{n,1}(index,5);
end
for jj=1:length(UDFES01_RON{n,1})
RHSTime=UDFES01_RON{n,1}(jj,1);
if isnan(RHSTime)
break;
end
index=(RHSTime*sampRate)+1;
UDFES01_RStepLength(jj,n)=UDFES01_HeelMarkers{n,1}(index,5)-UDFES01_HeelMarkers{n,1}(index,2);
end
UDFES01_MeanRStepLength(1,n)=mean(nonzeros(UDFES01_RStepLength(:,n)));
UDFES01_MeanLStepLength(1,n)=mean(nonzeros(UDFES01_LStepLength(:,n)));
end
This line:
UDFES01_LStepLength(ii,n)=UDFES01_HeelMarkers{n,1}(index,2)-UDFES01_HeelMarkers{n,1}(index,5);
throws the error "Index in position 1 is invalid. Array indices must be positive integers or logical values."
If I break it down into smaller pieces of code in the command window, I get this:
>> load('Heelmarkers.mat')
>> index
index =
1.7290e+03
>> UDFES01_HeelMarkers{n,1}(index,2)
Index in position 1 is invalid. Array indices must be positive integers or logical values.
>> UDFES01_HeelMarkers{n,1}(1.7290e+03,2)
ans =
0.7861
>>
So when I calculate the index, it is 1.7290e+03. Calling "index" in the code throws an error, but copying and pasting "1.729e+03" directly into the argument of the cell array does work. Similarly, if I use "1729" in the argument, it also works. I know that scientific notation is MATLAB's way of displaying values (not storing values), so I wouldn't think it would matter, but apparently it does. I have uploaded the.mat file in case anyone would like to try this for themselves.
In short, my question is: how can I calculate the index such that I will be able to use it in the code without getting an error?

Best Answer

So when I calculate the index, it is 1.7290e+03. Calling "index" in the code throws an error, but copying and pasting "1.729e+03" directly into the argument of the cell array does work.
You calculated index is displayed as 1729 but the value stored in the variable is actually very slightly different from 1729.
>> x = 0;
>> for k = 1:10
x = x + 0.1;
end
>> x
x =
1
>> d = x-1
d =
-1.11022302462516e-16
As stated on the second page of this Cleve's Corner article from 1996:
"Ten steps of length t are not precisely the same as one step of length 1. MATLAB is careful to arrange that the last element of the vector 0:0.1:1 is exactly equal to 1, but if you form this vector yourself by repeated additions of 0.1, you will miss hitting the final 1 exactly."
You're doing something similar, expecting your calculations to give an exact integer value but instead getting something that just misses hitting that integer value exactly.
Where possible, avoid computing array indices. If you must, compute using only operations on integer values that must give integer values if at all possible or round if not.
Related Question