Reputation: 9806
function create_simulation( matrix_all,timesteps,num_agents )
mkdir('movies');
filename_mov='.\movies\vid1';
vidObj = VideoWriter(filename_mov,'Motion JPEG AVI');
set(vidObj,'Quality',100,'FrameRate',2);
%matrix_all='time','id','xcor','ycor'
time=matrix_all(:,1);
id=matrix_all(:,2);
x=matrix_all(:,3);
y=matrix_all(:,4);
colors=jet(num_agents);
min_x=min(x);max_x=max(x);min_y=min(y);max_y=max(y);
f=figure('renderer', 'zbuffer');
a = axes('Parent',f);
axis(a,'tight');
set(a,'nextplot','replacechildren');
open(vidObj);
for t=1:timesteps,
t_filter=time==t;
scatter(x(t_filter),y(t_filter),[],colors,'filled');
xlim([min_x max_x]);
ylim([min_y max_y]);
xlabel(num2str(t));
drawnow;
writeVideo(vidObj, getframe(f));
end
close(vidObj);
end
The above code creates a movie just recording the first frame every time only. I have tried these answers,(as you can see in code) but no success.
Data(some of it):
>>matrix_all
1 1 680.640000000000 898.650000000000
1 2 754.610000000000 832.080000000000
1 3 864.500000000000 935.870000000000
1 4 752.080000000000 1023
1 5 728.080000000000 1052.10000000000
1 6 787.900000000000 1030.60000000000
2 1 678.170000000000 898.650000000000
2 2 754.610000000000 832.080000000000
2 3 864.500000000000 935.870000000000
2 4 752.080000000000 1023
2 5 728.080000000000 1052.10000000000
2 6 787.900000000000 1030.60000000000
3 1 678.170000000000 898.650000000000
3 2 754.610000000000 832.080000000000
3 3 864.500000000000 935.870000000000
3 4 752.080000000000 1023
3 5 728.080000000000 1052.10000000000
3 6 787.900000000000 1030.60000000000
4 1 678.170000000000 898.650000000000
4 2 754.610000000000 832.080000000000
4 3 864.500000000000 935.870000000000
4 4 752.080000000000 1023
4 5 728.080000000000 1052.10000000000
4 6 787.900000000000 1030.60000000000
>> create_simulation( matrix_all,4,6)
Worst case I will have to write all images to disk and create a movie of it.
Upvotes: 1
Views: 95
Reputation: 35525
You are writing the video correctly. The thing is, most of video players will not show all the frames saved in order to improve computational time. This also depedns a lot in the format you are saving the video on.
For example, if you save the video in 'Motion JPEG AVI'
you will get big compression, while of you save it in 'MPEG-4'
the compression is smaller.
The whole point here is: if you want to be able to see the video as it is, save it in a very little/none compressed file type. Also, never trust what VLC or any other video players plays for you, if you want to check the frames, load the video and check them.
You'll see that if you use vidObj = VideoWriter(filename_mov,'MPEG-4');
and open it in any video player, you will see the changes, but if you save it the way you do ant this way and you load them to MATLAB again, the frames are exactly the same.
Upvotes: 1