function  filelist = cache_PLE2_backgr(P,range,verbose)

%CACHE_PLE2_BACKGR -- Call MAKE_PLE2_BACKGR and write results to disk.
%
%  filelist = cache_PLE2_backgr(P,range,verbose)
%
%  P is a "stimulus parameter struct" produced by PLEXP2_PARAMS.
%  Some especially relevant fields are P.noise_params and P.NCache_params.
%
%  CACHE_PLE2_BACKGR generates a number of files in the current directory
%  with names such as 'L01_001.mat' ... 'R01_036.mat'.  Each file stores
%  a single variable -- NOISE_CACHE -- generated by MAKE_PLE2_BACKGR.
%  RANGE is a two-element vector specifying the first and last filename 
%  indices.  MAKE_PLE2_BACKGR is caled twice for each index: with 
%  BACKGR = 1("Left") and BACKGR = 2("Right").
%  VERBOSE -- fileid for verbose messages, 0=don't=default, 1=stdout.
%  The function returns a M-by-2 cell array of file names.
%
%  See also MAKE_PLE2_BACKGR, PLEXP2_PARAMS, PLEXP2.

%  Original coding by Alexander Petrov, UC Irvine.
%  $Revision: 1.1 $  $Date: 2003/08/06 18:56 $

%-- Decipher params
if (nargin<3)  verbose = 0 ; end    % don't print verbose messages

N = P.NCache_params.N ;           % number of noise patches to make (x2)
fname_str = P.NCache_params.fname ;      % cell array of format strings
N_cond = length(fname_str) ;      % two: 1="Left", 2="Right"
b = range(1) ;                    % typically 001
e = range(2) ;                    % typically 036
M = e-b+1 ;                       % number of file sets, typically 36
NcM = N_cond * M ;                % total number of files, typically 72

%-- Main loop
filelist = cell([M,N_cond]) ;
h = waitbar(0,'Generating filtered-noise patches, please wait...') ;
h = waitbar(0,h) ;
for  k = b:e
   for c = 1:N_cond
      %- Generate the file name
      fname = sprintf(fname_str{c},k) ;    % e.g., 'L01_001.mat'
      %- Call MAKE_PLE2_BACKGR to do the real work
      NOISE_CACHE = make_PLE2_backgr(P,c,N) ;    % <-- sic
      save(fname,'NOISE_CACHE') ;
      filelist{k-b+1,c} = fname ;
      if (verbose)  fprintf(verbose,'%s saved.\n',fname) ; end
      waitbar((N_cond*(k-b)+c)/NcM,h) ;
   end % for c = 1:N_cond
end % for k=1:N
close(h) ;

%--- Return FILELIST
%%%%% End of file CACHE_PLE2_BACKGR.M
