function [x, y, z] = simple_plot_3d(fname, stride, plot_type)
%  function [x, y, z] = simple_plot_3d(fname, stride, plot_type)
%  Reads in a three-column file and plots a surface plot of the data.
%  The input file is of the form:
%     z1  t1  p11
%     z1  t2  p12
%       ...
%     z1  tN  p1N
%     z2  t1  p21
%       ...
%  which is useful if you want to plot data of the form
%  distance, time, and power, as is useful with OCS.
%  In this demo, stride = N = number of time points in
%  the discretization.  

% As always, read in data and find its size:
data = load(fname);
[m n] = size(data);

% As before, do some checking for number of arguments:
if nargin == 1
  error('Not enough arguments!');
elseif nargin == 2
  plot_type = 'lin';
end

% We next have to reformulate the data into something matlab understands.
% Matlab's surf(X,Y,Z) command plots a surface of data where 
% X, Y, and Z are MxN matrices describing the X, Y, and Z data.
% Typically, the X matrix would have every row being the same, and 
% the Y matrix would have every column being the same for a plot
% like ours.  The following sets up the X, Y, and Z matrices as
% the variables time, distance, and power which are numZs x stride big.
numZs = m/stride;
time = ones(numZs,1)*data(1:stride,2)';          % Form a numZs x stride matrix of time
distance = data(1:stride:end,1)*ones(1,stride);  % Form a numZs x stride matrix of dist
power = reshape(data(:,3),stride,numZs)';        % Form a numZs x stride matrix of power

switch plot_type
  case {'lin'}
    surf(time,distance,power,'linestyle','none');
  case ('logz')
    surf(time,distance,log10(power),'linestyle','none');
  otherwise
    error('Unknown plot type');
end

% As we did in less_simple_plot, output the data into x, y, and z
% variables for convenience.
if nargout == 3
  x = time;
  y = distance; 
  z = power;
end

%% Questions: What are the pitfalls and deficiencies of this code?