function []=create_all_fig_files()

% Declar these to be global variables so they don't need to be passed to
% subfunctions.

global Nx
global Ny
global n_lines

% *************************************************************************
% File structure information

Nx=201;
Ny=101;

n_lines=Nx*Ny;

% *************************************************************************
% Grab all files in folder
% Here, files with names of the form "diffusion_*.dat" are examined.  The
% filenames of all such files are grabbed into a cell array called "files".
% The number of entries in the cell array is grabbed using the "numel"
% command.

files=dir('diffusion_*.dat');
klim=numel(files);

% *************************************************************************
% Initialize Matlab figure for plotting

% Here, I open a figure (h is a handle to a figure).
% However, I make it invisible - it is only defined in the background.
% This is a good idea for large jobs, especially if such jobs are to be run
% on a cluster with matlab.

h=figure;
set(h, 'Visible','off');

% Loop over all files from kk=1 up to the last file with index klim.

for kk=1:klim
    
    % Count the file and output it to the screen as a string message.
    display(strcat('drawing ',num2str(kk),'th file'))
    
    % Grab the name of the kkth file from the cell array.
    filename=files(kk).name;
    
    % Load the data from the kkth file into arrays of double-precision
    % numbers.
    
    % Here, the subfunction my_openfile() is very similar to the one we had
    % before: it opens the file of name "filename" and reads sequentially
    % through the lines of the file, gathering the data into vectors.  The
    % vectors are reshaped into two-dimensional arrays at the end of the
    % operation.
    
    [X,Y,C]=my_openfile(filename);
    
    % Standard contour plotting now.
    [~,cc]=contourf(X,Y,C);
    set(cc,'edgecolor','none')
    colorbar
    set(gca,'fontname','times new roman','fontsize',18)
    xlabel('x')
    ylabel('y')
    
    % Create a file name for the kkth figure.
    figfilename=strcat('contour_',num2str(kk),'.fig');
    
    % Save the kkth contour plot to a Matlab ".fig" file.
    saveas(h,figfilename)
    clf
end

end

% *************************************************************************
% *************************************************************************

function [X,Y,C]=my_openfile(filename)

% Global variables - I did not need to pass them explilcitly to the
% subfunction.

global Nx
global Ny
global n_lines

fid=fopen(filename);

X=0*(1:n_lines);
Y=0*(1:n_lines);
C=0*(1:n_lines);

for i=1:n_lines
    c1=fgetl(fid);
    vec_temp=sscanf(c1,'%f');
    x_temp=vec_temp(1);
    y_temp=vec_temp(2);
    C_temp=vec_temp(3);
    
    X(i)=x_temp;
    Y(i)=y_temp;
    C(i)=C_temp;
end

X=reshape(X,Nx,Ny);
Y=reshape(Y,Nx,Ny);
C=reshape(C,Nx,Ny);

fclose(fid);
    
end

% *************************************************************************