For loop is taking a lot of time to execute.

1 view (last 30 days)
I have to change four diagrams during the execution of one for loop. I'm using subplot to plot all the diagrams in one. But the loop takes too long for execution. I think it is because of the function "figure" being called every time. But if I'm not including figure after every loop execution it isn't showing the plot. All the variables in the code are matrices(eg. barax(i,;)). pos is the handle for the figure.
pos = figure('units','normalized','position',[0 0 1 1],'color','b',...
'menubar','none','numbertitle','off','name','WW_QuickReturn');
for i = 1:div
if strcmp(get(pb,'string'),'Start')
return
end
figure(pos)
%%Stimulation
subplot(2,2,1)
set(subplot(2,2,1),'XTickLabel',[],'YTickLabel',[],'XLim',[xmin xmax],'YLim',[ymin ymax],'XTick',0,'YTick',0)
hold on
title ('Stimulation','fontweight','bold','fontsize',20,'color','w')
cla;
plot(barax(i,:),baray(i,:),'b',barbx(i,:),barby(i,:),'g',barcx(i,:),barcy(i,:),'c','linewidth',5);
plot(0,0,'ok')
plot(0,0,'sk','MarkerSize',12)
plot(0,-1*d,'ok')
plot(0,-1*d,'sk','MarkerSize',12)
plot([min(x4)-20,max(x4)+20],[y4-10.5,y4-10.5],'k','linewidth',7)
fill(xset1(i,:),yset1(i,:),'r');
fill(xset(i,:),yset(i,:),'r');
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Velocity Diagram
subplot(2,2,2)
title ('Velocity Vector','fontweight','bold','fontsize',20,'color','w')
hold on
cla;
set(subplot(2,2,2),'XTickLabel',[],'YTickLabel',[],'XLim',[min(Vc) max(Vc)],'YLim',[1.5*min(Va*sin(t)) 1.5*max(Va*sin(t))],'XTick',0,'YTick',0)
quiver(0,0,Va*sin(-t(i)),Va*cos(-t(i)),0)
h1 = text(Va*sin(-t(i))/2,Va*cos(-t(i))/2,'Va');
set(h1,'Rotation',-t(i)*180/pi);
quiver(Va*sin(-t(i)),Va*cos(-t(i)),-Vc(i)-Va*sin(-t(i)),-Va*cos(-t(i)),0)
h2 = text((-Vc(i)+Va*sin(-t(i)))/2,(Va*cos(-t(i))/2),'Vca');
set(h2,'Rotation',atan(-Va*cos(-t(i))/2/(Vc(i)-Va*sin(-t(i))/2)));
quiver(0,0,-Vc(i),0,0)
text(-Vc(i)/2,0,'Vc');
hold off
end
Can anyone suggest how to reduce the execution time.
  6 Comments
Dhruv Thakkar
Dhruv Thakkar on 14 Nov 2017
Edited: Dhruv Thakkar on 14 Nov 2017
Geoff Hayes - I used drawnow removing the "figure(pos)" line but there wasn't much of a difference. With "figure(pos)" it took 0.13sec/iteration, now it takes 0.09sec/iteration.
If I can further reduce the time it would be very useful for me. Thank you.
PS. pause and drawnow gave nearly the same result.
Geoff Hayes
Geoff Hayes on 14 Nov 2017
ok, so try updating the plot on each iteration of the loop. here is a quick example
x = -2*pi:0.01:2*pi;
y = sin(x);
figure;
hPlot = plot(NaN,NaN);
xlim([x(1) x(end)]);
ylim([min(y) max(y)]);
for k=1:length(x)
xdata = [get(hPlot,'XData') x(k)];
ydata = [get(hPlot,'YData') y(k)];
set(hPlot,'XData',xdata,'YData',ydata);
pause(0.01);
end
Note how we call plot once and then update the x and y data for that plot on each iteration of the loop.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!