function velocity_arrows_sequence2 ( xyuv_filename, scale ) %% VELOCITY_ARROWS_SEQUENCE2 creates velocity plots from a sequence of datasets. % % Discussion: % % This program can be used to turn data files into a sequence of PNG % images, suitable for an animation. % % Although this program can handle a single input file, its most useful % application is in processing many files, with 'consecutive' names, such % as "fred01.txt", "fred02.txt", "fred03.txt" and so on. The user only % has to tell the program to process the first file, and it will automatically % continue to process files until it reaches the end of the sequence of names. % % The input files: % =============== % % Each XYUV input data file is assumed to store the value of the X, Y % coordinates and horizontal and vertical velocity at a point, one set % of values per line. % % Internal processing: % =================== % % Once the first file is processed, the routine checks to see if there % is a second "consecutive" file, and it so, it is also processed, and % so on, until presumably all files in the sequence have been handled. % % Usage: % % velocity_arrows_sequence2 ( xyuv_filename, scale ) % % Note that a literal filename must be enclosed in single quotes. % Thus, a typical invocation might be % % velocity_arrows_sequence2 ( 'xyuv0001.txt', 1.0 ) % % But if you simply say % % velocity_arrows_sequence2 % % the program will give you a chance to enter the data interactively. % % Modified: % % 18 November 2005 % % Author: % % John Burkardt % % Parameters: % % Input, string XYUV_FILENAME, the name of the (first) XYUV input file. % % Input, real SCALE, the scale factor for the arrow size. Setting SCALE to 1 % results in the default vector size. % fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE2:\n' ); fprintf ( 1, ' Given a sequence of XYUV files,\n' ); fprintf ( 1, ' display the velocity fields\n' ); fprintf ( 1, ' and save each image in a PNG file.\n' ); % % If at least one command line argument, it's the XYUV file name. % if ( nargin < 1 ) fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE2:\n' ); xyuv_filename = input ( ' Enter the XYUV file, such as ''xyuv.txt'':' ); end if ( ~file_exist ( xyuv_filename ) ) fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE2 - Fatal error!\n' ); fprintf ( 1, ' The file "%s" does not exist.\n', xyuv_filename ); return end % % If two command line arguments, the second is the scale factor. % if ( nargin < 2 ) fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE2:\n' ); scale = input ( ' Enter the scale factor:' ); end % % Save the name of the first input file, and set the name of the first PNG file. % xyuv_root = xyuv_filename; png_root = file_name_ext_swap ( xyuv_root, 'png' ); png_filename = png_root; fprintf ( 1, '\n' ); fprintf ( 1, ' The first PNG file will be %s\n', png_root ); % % Determine the X and Y range, and the U, V maximum magnitude over all the files. % x_min = inf; x_max = -inf; y_min = inf; y_max = -inf; mag_max = 0.0; xyuv_filename = xyuv_root; while ( 1 ) xyuv = load ( xyuv_filename ); x = xyuv(:,1); y = xyuv(:,2); u = xyuv(:,3); v = xyuv(:,4); x_min = min ( x_min, min ( x ) ); x_max = max ( x_max, max ( x ) ); y_min = min ( y_min, min ( y ) ); y_max = max ( y_max, max ( y ) ); mag = sqrt ( u(:).^2 + v(:).^2 ); mag_max = max ( mag_max, max ( mag ) ); xyuv_filename = file_name_inc ( xyuv_filename ); if ( ~file_exist ( xyuv_filename ) ) break end end if ( mag_max == 0.0 ) fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE2 - Fatal error!\n' ); fprintf ( 1, ' The input data is entirely zero.\n' ); fprintf ( 1, ' No plot is possible.\n' ); error ( 'VELOCITY_ARROWS_SEQUENCE2 - The input data is zero!' ); end delta = 0.05 * max ( x_max - x_min, y_max - y_min ); x_frame = [ x_min - delta, ... x_max + delta, ... x_max + delta, ... x_min - delta, ... x_min - delta ]; y_frame = [ y_min - delta, ... y_min - delta, ... y_max + delta, ... y_max + delta, ... y_min - delta ]; % % Create the plots. % xyuv_filename = xyuv_root; frame_num = 0; % % Loop over all files. % while ( 1 ) xyuv = load ( xyuv_filename ); x = xyuv(:,1); y = xyuv(:,2); u = xyuv(:,3); v = xyuv(:,4); quiver ( x, y, u, v, scale / mag_max, 'b' ); axis equal hold on k = convhull ( x, y ); plot ( x(k), y(k), 'r' ); hold on x_min = min ( x ); x_max = max ( x ); y_min = min ( y ); y_max = max ( y ); delta = 0.05 * max ( x_max - x_min, y_max - y_min ); plot ( x_frame, y_frame, 'w' ); hold off xlabel ( 'X', 'FontName', 'Helvetica', 'FontWeight', 'bold', ... 'FontSize', 16 ); ylabel ( 'Y', 'FontName', 'Helvetica', 'FontWeight', 'bold', ... 'FontSize', 16, 'Rotation', 0 ); title ( xyuv_filename, 'FontName', 'Helvetica', 'FontWeight', ... 'bold', 'FontSize', 16 ); % % Here's where we take a snapshot of the current image, and save it to % a PNG file. Doing this was surprisingly complicated, and the help % system was not very clear. Surely there's a more straightforward way! % F = getframe; [ XX, map ] = frame2im ( F ); imwrite ( XX, png_filename, 'PNG' ); frame_num = frame_num + 1; % % Prepare for next iteration. % xyuv_filename = file_name_inc ( xyuv_filename ); if ( ~file_exist ( xyuv_filename ) ) break end png_filename = file_name_inc ( png_filename ); end fprintf ( 1, '\n' ); fprintf ( 1, ' Processed %d frames.\n', frame_num ); fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE2:\n' ); fprintf ( 1, ' Normal end of execution.\n' );