Delay in for loop

13 views (last 30 days)
Joe Ajay
Joe Ajay on 28 Dec 2012
Hi,
This is my code. executing a function and writing the values in an excel file.
After certain iterations 'n' it has to find the average of the values and that is how I planned.
But somewhere it went wrong and when I give 'n' as 2. i.e the no.of iteration for for loop be 2.
Instead of getting the result with in 5 or 7 seconds (when I do not calculate the average) The loop doesn't stop also it doesn't give any error msg.
When I close the Matlab window and open the written file there I get my answer written in the excel file.
What makes the for loop to take so long and what happens during that time.
Is there any way I can get the average of the values it.
Thanks
for i=1:n
tic;
[X,FVAL] = ga(@func1a,4,[],[],[],[],[-2 500 45 200],[2 1800 72 1600],[],optimset('TolX',1e-12));
t=toc;
if (i==1)
y(i,:)=[[],[],X,t];
cel = sprintf('A%i', i);
w = {'GA','Trail.No','A','B','C','D','Time';[],i,X(1),X(2),X(3),X(4),t};
xlswrite('d:\Office.xls',w,1,cel);
else
y(i,:)=[[],[],X,t];
cel = sprintf('A%i', i+1);
a={[],i,X(1),X(2),X(3),X(4),t};
xlswrite('d:\Office.xls',a,1,cel);
while i==n
a=sum(y)/n;
cel = sprintf('A%i', i+2);
xlswrite('d:\Office.xls',a,1,cel);
end
end
end

Answers (1)

Walter Roberson
Walter Roberson on 28 Dec 2012
In your loop you have
while i==n
a=sum(y)/n;
cel = sprintf('A%i', i+2);
xlswrite('d:\Office.xls',a,1,cel);
end
but inside that loop you do not change "i" and you do not change "n" so if the loop condition is ever true it will remain true and you infinite loop.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!