# Iteration loops

1. May 12, 2010

### ShaunaLB

I have a code in Matlab where I need to measure the radiation on a specific latitude over a year. I have my time steps in hours and I am iterating over a year. I have parameters for the amount of radiation per hour in a 24 hour period, but my problem is that I want to loop this 24 iteration loop 365 times. Any suggestions??

2. May 12, 2010

### Topher925

Yes, just use a nested loop. For example;

Code (Text):
for d = 1:365
for h = 1:24
blahh....
end
end

3. May 12, 2010

### ShaunaLB

thanks,
I also have a reocurring problem with my index being out of bounds. here is my code
clear
clf
n=8760;
dt=1/24;
Vp=2.9;
alpha=0.025;

for i=1:n
for j=1:24
if j>=1 && j<5;
I(j)=1;
end
if j>=5 && j<7;
I(j)=978 +1;
end
if j>=7 && j<17;
I(j)=1955.7;
end
if j>=17 && j<19;
I(j)=-978 + 1955.7;
end
if j>=19 && j<=24
I(j)=1;
end
end
F(i)=5*I(i);

end
plot (F)

and I keep getting the error:
??? Attempted to access I(25); index out of bounds because
numel(I)=24.

Error in ==> Itest2 at 26
F(i)=5*I(i);

if the issue of index out of bounds could also be explained that would be greatly helpful

4. May 12, 2010

### Topher925

The problem is that you are trying to access a dimension of the array I that doesn't exist. You cant call the value form I(25) because it only goes to 24.

Also, you need to structure your code better. Matlab isn't fortran or the original C. For loops should never contain "and" statements. If you want to loop from value 19 to 24 for example, then go like this;

Code (Text):

for j = 19:24
do stuff....
end

%or for steps of 2

for j = 18:2:24
do stuff
end
I recommend reading some matlab basic tutorials to help get you up to speed with programming.