MATLAB plotting loop causes error and hold does not continue when arrows are included in plot

7 views (last 30 days)
I am trying to write a movie of a series of plot (basically I want to show/make a video of this plot "being drawn"). What happens is when I try to have a textarrow in the plot, I get an error somewhere in my loop (not always the same index) and the script stops working. When I comment out the textarrow, it does work. But I really want to have this annotation!
EDIT 1: I realized if I don't click anywhere while the script runs, I don't get this error. However, the script stops actually plotting the data points at some random point. It just shows the axis and no data randomly (~2 seconds into the movie file) and then for the rest of loop/movie doesn't plot any points.
EDIT 2: Looks like this issue first occurs at the 151st iteration
Here is the code, with the arrow annotations in comments:
close all
writerObj = VideoWriter('K_ac.avi')
writerObj.FrameRate = 70;
writerObj.Quality = 100;
open(writerObj);
figure1=figure('Color',[0 0 0],'position',[.1 .1 1232 639])
for i=1:513%513;
j=i+205;
plot(t_A(j,:),k_A(j,:), 'Color', [94/153,60/153,153/153],'linewidth',3)
hold on
plot(t_C(i,:), k_C(i,:), 'Color', [230/230,97/230,1/230],'linewidth',3)
hold on
xlabel('Time','FontSize',20,'Color','White','FontName','Avenir')
ylabel('K (Average)','FontSize',20,'Color','White','FontName','Avenir')
xlim([1,3.5])
ylim([0,.004])
set(gca,'Color',[0 0 0],'xcolor','w','ycolor','w');
h=legend('Droplet-Free','Droplet-Laden','location','southeast');
set(h,'TextColor', 'white','FontSize',18,'FontName','Avenir')
set(gca,'xticklabel',{[]})
set(gca,'yticklabel',{[]})
set(gca, 'TickLength', [0 0]);
% Create textarrow 1
% annotation(figure1,'textarrow',[0.570386904761905 0.552529761904762],...
% [0.789686101295642 0.674078327444052],...
% 'TextEdgeColor',[0 0 0],...
% 'TextLineWidth',1,...
% 'HorizontalAlignment','center',...
% 'FontSize',18,...
% 'FontName','Avenir Next',...
% 'String',{'Droplet-Free'},...
% 'HeadLength',20,...
% 'HeadWidth',22,...
% 'LineWidth',1,...
% 'Color',[1 1 1]);
%
% % Create textarrow 2
% annotation(figure1,'textarrow',[0.388511904761906 0.41125],...
% [0.392186101295646 0.518333333333333],'TextEdgeColor',[0 0 0],...
% 'TextLineWidth',1,...
% 'HorizontalAlignment','center',...
% 'FontSize',18,...
% 'FontName','Avenir Next',...
% 'String',{'Droplet-Free'},...
% 'HeadLength',20,...
% 'HeadWidth',22,...
% 'LineWidth',1,...
% 'Color',[1 1 1]);
drawnow
frame = getframe(gcf);
writeVideo(writerObj,frame);
end
close(writerObj);
And when the arrows are uncommented some way through the loop it freezes and when I click on the MATLAB window I get this error:
Error using VideoWriter/writeVideo (line 383)
Frame must be 1232 by 639
Error in K_ac (line 77)
writeVideo(writerObj,frame);
If anyone has a recommendation of how to work around this, please let me know! Thank you very much for your time and help!

Accepted Answer

Mishaal Aleem
Mishaal Aleem on 30 Aug 2015
Found the solution. Will post here incase anyone comes across this later and wants the answer. It turns out it was a memory issue. The way I got around this was closing the figure in the loop, and then replotting in the next iteration.
So the plot commands were changed to:
plot(ax, t_A(1:j),k_A(1:j), 'Color', [94/153,60/153,153/153],'linewidth',3)
hold on
plot(ax, t_C(1:i), k_C(1:i), 'Color', [230/230,97/230,1/230],'linewidth',3)
And at the end of the loop I added the line to close the figure before starting the next iteration!
drawnow
frame = getframe(gcf);
writeVideo(writerObj,frame);
close(gcf);
end

More Answers (2)

Dinesh Iyer
Dinesh Iyer on 26 Aug 2015
The error appears to indicate that the you are attempting to write a frame having different dimensions than the ones that have been written previously.
A quick check would be to display size(frame) for each frame that you are obtaining using getframe(gcf). Make sure all frame sizes match. This will also help you verify if the error occurs only when there is a mismatch.
Dinesh
  1 Comment
Mishaal Aleem
Mishaal Aleem on 26 Aug 2015
Edited: Mishaal Aleem on 26 Aug 2015
Dinesh,
Thanks for this tip. I ran it with the displayed frame size. This helped me realized that the error occurs if I click outside the window. If I don't click, the script continues - but there is still an error :
The script stops actually plotting the data points at some random point. It just shows the axis and no data randomly (~2 seconds into the movie file) and then for the rest of loop/movie doesn't plot any points.
If anyone has any tips, please let me know! Thank you

Sign in to comment.


Walter Roberson
Walter Roberson on 26 Aug 2015
You need to parent all of your graphics operations; see http://uk.mathworks.com/matlabcentral/answers/22208-show-figure
Assume that "current" (current figure, current axis) can change at any time, so you need to specify your target for every graphics operation.
  1 Comment
Mishaal Aleem
Mishaal Aleem on 26 Aug 2015
Hi Walter,
Thanks for the comment.
I tried to update my script by changing the first few lines to:
ax = axes('Parent',figure1);
for i=1:513%513;
j=i+205;
plot(ax, t_A(j,:),k_A(j,:), 'Color', [94/153,60/153,153/153],'linewidth',3)
hold on ax
plot(ax, t_C(i,:), k_C(i,:), 'Color', [230/230,97/230,1/230],'linewidth',3)
hold on ax
And elsewhere, replacing gca with ax.
However, as I mentioned to Dinesh, the problem doesn't seem to be the size. If I don't click outside the matlab figure window, it keeps on going through the loop. However, at i=150ish, the plot stops plotting data, all previous data points disappear, and the left and bottom axis disappear. After this, the loop simply does not plot anything (i.e. I have a "empty" plot for the rest of 400ish loop cycles, although the legend, arrow, and axes labels are there. The lines of the bottom and left of the axes are gone, however.
Any insight you have is appreciated! Thanks!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!