Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

Geoff Hayes
Geoff Hayes le 27 Oct 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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by