The following MATLAB script is a solution to the example in the HPSC tutorial on Color in Scientific Visualization web page:
Example [advect1]:
Using MATLAB or IDL, produce mesh surface, shaded surface, and contour plots of the data in the file advect.dat. You need to use IDL or MATLAB input/output functions.
One solution to this example is the MATLAB script, advect1.m, shown below.
This program uses three MATLAB I/O functions: fopen, fscanf, and fclose. These are similar to the I/O functions of the same names in the C programming language:
Since the data set may take some time to be read and manipulated by MATLAB, the script contains a few statements containing the display function, disp, to inform the user of the progress through the script. Otherwise the wait might seem too long, and the user might think that there was a problem with the machine.
The matrix, Z, is used for two different purposes in the script; this is done to conserve memory usage, as the data set is quite large.
Two plot windows are created by this script. One of these contains subplots. This allows you to view all three plots on the screen at once. The shaded surface plot from this script is given in the figure on the main Web page.
% advect1.m
% Solution to Example 2.8.1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This work has been supported by the National Science Foundation %
% under an Educational Infrastructure grant, CDA-9017953. It has %
% been produced by the HPSC Group, Department of Computer Science, %
% University of Colorado, Boulder, CO 80309. Please direct %
% comments or queries to Elizabeth Jessup at this address or e-mail %
% jessup@cs.colorado.edu. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read and plot advection data for 50x50 grid
disp('Example 2.8.1') ;
% Read data file into matrix Z
advdat = fopen ('advect.dat', 'r') ;
Z = [ ];
disp('Reading data from advect.dat')
for i=1:2500
[arow] = fscanf (advdat, '%g %g %g', [3 1]) ;
Z = [Z ; arow'] ;
end
fclose (advdat) ;
% Separate the displacements from Z and
% put into vector named R
disp('Data is read -- Separating data')
R = Z(:,3);
% Create 50x50 grid Z from 1-D R, row by row
% Z is reused to save memory
Z = [ ];
disp('Constructing grid Z')
for i=1:50
first = (i-1)*50 + 1;
last = (i-1)*50 + 50;
S = R(first:last);
Z = [Z; S'];
end
% And plot
disp('Plotting')
w10 = figure ('Position', [5, 5, 545, 400]) ;
surf(Z)
print advect1surf -dps
w11 = figure ('Position', [5, 550, 545, 400]) ;
subplot(211), mesh(Z);
subplot(212), contour(Z);
disp('Done!');