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

5 Ansichten (letzte 30 Tage)
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?
  2 Kommentare
Margo Donlin
Margo Donlin am 7 Nov. 2019
I did also try using
index=ceil((LHSTime*sampRate)+1);
which worked for some indices, but sometimes it also increased the index by one, which is then wrong. On a different dataset it created an index that was outside of the bounds of the array, which caused another problem. For this specific case,
index=floor((LHSTime*sampRate)+1);
would work, but again, this didn't work for all of the cases.
Guillaume
Guillaume am 7 Nov. 2019
I know that scientific notation is MATLAB's way of displaying values (not storing values)
It's one of the way available to you. I think that it being the default is a bit silly but you can change it. I prefer
format shortg
or if I need to see more precision
format longg
either of which only use scientific notation when it's really necessary
>> format('short'); index
index =
1.7290e+03
>> format('shortg'); index
index =
1729
>> format('longg'); index
index =
1729.0001

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Steven Lord
Steven Lord am 7 Nov. 2019
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.
  2 Kommentare
Margo Donlin
Margo Donlin am 7 Nov. 2019
Thank you! I'm a little sad to say that I thought of using floor and ceil but not round. As for computing indices, my choices were to manually enter time stamp data into all of my data so I could use find or calculate the index from the time.
Steven Lord
Steven Lord am 7 Nov. 2019
If you have time-based data, take a look at the timetable data type. You can index into that with time ranges. Let's make some sample data.
today = datetime('today');
startOfMonth = dateshift(today, 'start', 'month');
endOfMonth = dateshift(today, 'end', 'month');
daysOfMonth = (startOfMonth:days(1):endOfMonth).';
data = (1:numel(daysOfMonth)).'.^2;
Now I'll build a timetable using that sample data.
tt = timetable(daysOfMonth, data)
Let's get the data starting two days ago to two days from now. The 'closed' option makes the timerange include both the start and end dates/times.
fiveDays = timerange(today-days(2), today+days(2), 'closed');
tt(fiveDays, :)
You may also be interested in the retime, synchronize, rowfun, and/or varfun functions.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

the cyclist
the cyclist am 7 Nov. 2019
>> format long
>> index
index =
1.729000100000000e+03
index is not equal to 1729.

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by