%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Author: Rachel Cannara, Engineering Physics Department, University of
%Wisconsin-Madison, 1500 Engineering Dr., Madison, WI 53706.
%This function/script is authorized for use in government and academic
%research laboratories and non-profit institutions only. Though this
%function has been tested prior to its posting, it may contain mistakes or
%require improvements. In exchange for use of this free product, we 
%request that its use and any issues that may arise be reported to us. 
%Comments and suggestions are therefore welcome and should be sent to 
%Prof. Robert Carpick <carpick@engr.wisc.edu>, Engineering Physics 
%Department, UW-Madison.
%Date posted: 7/8/2004
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function proc = batch_process(fun,file_name,endno)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This function processes a batch of DI data files defined by the file names of the first
%file index in the series and the final index (nof). The 'fun' argument is the function used in
%the process; it must take only one input (file_name), e.g., 'RCB2804B.480', unless otherwise 
%edited, e.g., reading load offsets. nof and endno should not be in quotation marks and can be 
%one to three digits long. All data files to be processed should first be moved or copied to a folder 
%containing batch_process.m, the process function ('fun'.m), and all related functions.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%The commented lines below read in the load offsets (deflection in volts from a file before 
%processing, e.g., friction vs. load data. Use load_offset_batch.m to generate the load
%offset files and be sure to uncomment the code.

k = findstr('.',file_name);
no1 = str2num(file_name(k+1:k+3));
[pathstr,name,ext,versn] = fileparts(file_name);
fun = str2func(fun);
%new_file = fullfile(pathstr,[name num2str(no1) '-' num2str(endno) 'L']);
%off = load(new_file);

%if (length(off) ~= (1+endno-no1))
%    disp('Error: incorrect load-offset array size. Check answer for array.')
%    proc = off;
%else
    go = 1;
    while (go == 1)
        for (n = no1:endno)
            disp(['--> Processing file ' num2str(n) '...'])
            index = num2str(n);
            if (n < 10) 
                new_ext = strcat('00',index);
            elseif (n >=10) & (n < 100)
                new_ext = strcat('0',index);
            elseif (n >= 100)
                new_ext = index;
            end
            full_name = strcat(name,'.',new_ext);
%            A = feval(fun,full_name,off(n+1-no1));
            A = feval(fun,full_name);
            cont = input('Continue processing files? (no = 0) '); %set cont=1 to skip this prompt.
            if (cont == 0)
                go = 0;
                break
            end
        end
        go = 0;
    end
    proc = n - no1 + 1;
    disp(['Number of files processed = ' num2str(proc)])
    %end
