creating a loop to save images from contour slice

I have the following code to plot a slice at any location in a fluid volume.

clc, clear all, close all
format long
%a ddpath('\\ds.leeds.ac.uk\staff\staff6\censsar\Polydisperse'); % <------Change

%% Import data
input = importdata('lci_000210.dat',' ',3);
nx = 96;
ny = 96;
nz = 49;

x = input.data(:,1);
y = input.data(:,2);
z = input.data(:,3);
Lci = input.data(:,4);

L3d = reshape(Lci,[nx,ny,nz]); 
x3d = reshape(x,[nx,ny,nz]); 
y3d = reshape(y,[nx,ny,nz]); 
z3d = reshape(z,[nx,ny,nz]); 

contourslice(y3d,x3d,z3d,L3d,[],[],[0.3]);
ax=gca;
ax.Children(1).LineStyle='none';
ax.Children(2).LineStyle='none';
ax.Children(3).LineStyle='none';
view(25,20);
colormap jet
colorbar

What I would like to do is have the contour slice in a loop so that it creates and saves a slice at each z location. I want to hopefully create a movie of the z slice moving from zero and going up the 3d axis.

I have tried something like:

Z=[0 0.1 0.5];
for S = 1:length(Z)
    h = figure
    contourslice(y3d,x3d,z3d,L3d,[],[],[Z],10); 
    saveas(h,sprintf('Fig%d.png',S));
end

but this is not working, I am not sure how to define the z axis in the loop, so it creates a slice at each point.

Here is the link to my data, it is a .dat file so it contains the data in 4 columns.

Upvotes: 0

Views: 211

Answers (1)

il_raffa
il_raffa

Reputation: 5175

To create a set of slices in a loop you have to modify your code by replacing the last but one parameter in the call to contourslice by specifiybg the i-th element of the array Z.

Since you did not post your input data I've tested the proposed solution on an example data from MatLab contourslice help slightly modified in which

are created nine contour plots in the y-z plane, no plots in the x-z plane, and one plot in the x-y plane by specifying Sx as a vector of nine elements, Sy as an empty vector, and Sz as a scalar (from MatLab help).

Yoy have to adapt the definition of the Sx, Sy and Sz parameters to your needs.

To create a movie you can use the functions:

  • videowriter to create the video object
  • open to open the video file
  • getframe to capture axes or figure as movie frame
  • close to close and save the video file

As an alternative to the movie, you can create an animated gif by using the function imwrite

% Load input data
[X,Y,Z,V] = flow;
% Define the parameters for the set of slices
Sx = 1:9;
Sy = [];
Sz = [];
cvals = linspace(-8,2,10);
% Open the FIGURE window
figure
% Create the axes and set tehiur properties
axis([0,10,-3,3,-3,3])
hold on
daspect([1,1,1])
campos([0,-20,7])
box on
% Create the movie object
mov=VideoWriter('contour_slice_movie.avi');
% Open the movie file
open(mov);
% Define the number of frames to be captured for each slice
n_frame_x_image=33;
% Loop over the desired number of slices
for i=1:length(Sx)
   contourslice(X,Y,Z,V,Sx(i),Sy,Sz,cvals)
   % Capture the frames
   for j=1:n_frame_x_image
      FF=getframe(gcf);
      writeVideo(mov,FF);
   end
end
% Close the movie file
close(mov);

enter image description here

Upvotes: 1

Related Questions