MATLAB plotting loop causes error and hold does not continue when arrows are included in plot
Mostrar comentarios más antiguos
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!
Respuesta aceptada
Más respuestas (2)
Dinesh Iyer
el 26 de Ag. de 2015
0 votos
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 comentario
Mishaal Aleem
el 26 de Ag. de 2015
Editada: Mishaal Aleem
el 26 de Ag. de 2015
Walter Roberson
el 26 de Ag. de 2015
0 votos
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 comentario
Mishaal Aleem
el 26 de Ag. de 2015
Categorías
Más información sobre Image Arithmetic en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!