Filter löschen
Filter löschen

Why do I get a for loop error "Subscript indices must either be real positive integers or logicals"?

1 Ansicht (letzte 30 Tage)
I have the following code (below) that uses a for loop. Why do I get the error "Subscript indices must either be real positive integers or logicals"?
data.Year=year(data.Date);
data.Month=month(data.Date);
data.Day=day(data.Date);
data.Hour=hour(data.Date)+1;
data.Wday=weekday(data.Date);
data.Bday=isbusday(data.Date);
data.Hday=1*((data.Bday==0)&((data.Wday~=1)&(data.Wday~=7)));
data.Hday2=data.Hday;
[rows, cols]=size(data.Hday);
for m=1:rows
if (data.Hday(m)==1)&&((data.Month(m)==12)||(data.Month(m)==11)||(data.Month(m)==9)|(data.Month(m)==7)|(data.Month(m)==5))
data.Hday2(m)=data.Hday(m)+1;
data.nday(m)=0;
elseif (data.Hday(m)==1)&&(data.Month(m)==1)&&(data.Day(m)<8)
data.Hday2(m)=data.Hday(m)+1;
data.nday(m)=0;
elseif (data.Hday(m)==1)
data.Hday2(m)=1;
data.nday(m)=0;
elseif (data.Hday(m-1)==1)&&(data.Month(m-1)==11)
data.Hday2(m)=.5
else
data.Hday2(m)=0;
data.nday(m)=1;
end
end

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 27 Okt. 2017
The problem is because of m
for m=1:rows
Note how m starts at 1. Now look at this line
elseif (data.Hday(m-1)==1)&&(data.Month(m-1)==11)
You are subtracting 1 from m and so when m is one, then m-1 is zero which is not a real positive integer. MATLAB arrays are one-based and so the minimum index into an array is one.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by