Kelvin S
Kelvin S

Reputation: 351

Creating a video from a set of 3D plots

I used a function called ind2patch to make a 3D block which contains a number of smaller blocks in 3 dimensions. Each small block has a value that is represented by a color. A typical plot is like this one:

typical plot

Now I would like to show the evolution of values (i.e. the color) with time of these small blocks using a video. I have data at different moments but I only know how to plot the graphs at different time by reading different files. Is there a way to combine the plots to a video or directly plot the graphs in the form of video?

Here is my code:

clear; close all; clc;
fig = figure(1); 
set (fig, 'Units', 'normalized', 'Position', [0,0,1,1]);
fig_color='w'; fig_colordef='white';
cMap=jet(256); 
faceAlpha1=1;
faceAlpha2=0.65;
edgeColor1='none';
edgeColor2='none';
NumBoxX=100;%box number in x direction
NumBoxY=100;%box number in y direction
NumBoxZ=5;%box number in z direction

fid = fopen('rho  20950.dat','r');
datacell = textscan(fid, '%f%f%f%f%f%f%f%f'); 
fclose(fid);

all_data = cell2mat(datacell); 

M=zeros(NumBoxX,NumBoxY,NumBoxZ); 

for i=1:NumBoxX            
    for j=1:NumBoxY        
        for k=1:NumBoxZ     
            num=k+NumBoxZ*(j-1)+NumBoxZ*NumBoxY*(i-1);
            M(i,j,k)=all_data(num,4); 
        end
    end
end

indPatch=1:numel(M);
[F,V,C]=ind2patch(indPatch,M,'v'); 

title('\sigma_{xy} in different cells','fontsize',20);
xlabel('y','fontsize',20);ylabel('x','fontsize',20); zlabel('z','fontsize',20); hold on;
set(get(gca,'xlabel'),'Position',[5 -50 30]); 
set(get(gca,'ylabel'),'Position',[5 50 -15]);
set(get(gca,'zlabel'),'Position',[64 190 -60]);
patch('Faces',F,'Vertices',V,'FaceColor','flat','CData',C,'EdgeColor','k','FaceAlpha',0.5);
axis equal; view(3); axis tight; axis vis3d; grid off;
colormap(cMap); caxis([min(M(:)) max(M(:))]);
cb = colorbar;                                     

set(get(cb,'title'),'string','Stress (MPa)','fontsize',20); 

lbpos = get(cb,'title'); % get the handle of the colorbar title
%set(lbpos,'Units','data');% change Units to data
%pos = get (lbpos,'position'); % get position of the colorbar title
set(lbpos,'units','normalized','position',[0,1.04]);

MyAxes=gca;
set(MyAxes,'Units','Normalized','position',[0.05,0.1,0.8,0.8]);
zoom(1.85); 

Upvotes: 1

Views: 179

Answers (1)

kkuilla
kkuilla

Reputation: 2256

You could do something like this:

  1. Loop through each patch and grab an image of it.
  2. Insert the images into a matrix
  3. Convert the image matrix into a movie using immovie

% // Create a matrix to hold your images A = zeros(row,col,numOfColours, numOfFrames);

where row is the number of rows and col is the number of columns in one image.

Loop through your patches and create a video of the individual images.

for n=1:numOfPatches
    imshow(patches(:,:,n)) % // display the image
    frame = getframe(gcf) % // get the current figure window
    im = frame2im(frame); % // convert it to an image
    A(:,:,1:3,n)  = im;      % // Insert the image into the matrix
end

The you can use immovie to convert it to a movie

mov = immovie(RGB);
movie(mov); % // play the movie

Upvotes: 4

Related Questions