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')>> indexindex = 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