% Jake Bobowski
% June 8, 2016
% Created using MATLAB R2014a.

% In this MATLAB script we attempt to develop a scheme to import and then
% process data from multiple files.

% Clear all variable assignments.
clearvars

% Before importing multiple files, we first have to learn about some of the
% properties of 'sprintf'.  sprint(%f, n) outputs the floating point number
% n.
sprintf('%f',3.21)

% If we put a number m in front of the f, it specifies that the number
% output should contain 24 characters.  In the example below, there's lots
% of white space in front of the output.
sprintf('%24f',3.21)

% We can force the white space to be filled with zeros by putting a 0
% (zero) in front of the number m.
sprintf('%024f',3.21)

% We can also specify the number of characters that should follow the
% decimal point using .p before the f where p is a postive integer.
sprintf('%024.6f',3.21)
sprintf('%024.6f',321)

% Suppose our files a labelled something like file001data.txt,
% file002data.txt, ...  We can use sprintf inside a loop to make the 001,
% 002, ...  We want out numbers to be m = 3 characters long and to have p =
% 0 characters after the decimal place.
sprintf('%03.0f',1)
sprintf('%03.0f',2)
sprintf('%03.0f',3)

% Inside a loop:
for k = 1:5
    sprintf('%03.0f',k)
end

% We can also put text within our sprintf command:
for k = 1:5
    sprintf('file%03.0fdata.txt',k)
end

% Finally we combine our sprintf command with dlmread to import multiple
% files.  In the example below I am assuming that the data files are all
% contained within the same folder as this MATLAB script (.m file).
for k = 1:5
    filename = sprintf('file%03.0fdata.txt', k);
    mydata{k} =  dlmread(filename);
end

% mydata{} is called a cell.  We access the first element using mydata{1}.
% This first element is a column vector.  We transpose it to a row vector
% using mydata{1}'
mydata
mydata{1}
first = mydata{1}'

% Maybe we only want every second file to be imported.  Only a slight
% change to the for loop control statement is required.
for k = 1:2:5
      filename = sprintf('file%03.0fdata.txt', k);
      myODDdata{k} =  dlmread(filename);
end
myODDdata
for k = 2:2:5
      filename = sprintf('file%03.0fdata.txt', k);
      myEVENdata{k} =  dlmread(filename);
end
myEVENdata

% If you want to select only specific files to be imported using
% non-sequential numbers, you can do the following.
importList = [1,2,3,4,5,8,12];
for k = 1:length(importList)
    filename = sprintf('file%03.0fdata.txt', importList(k));
    mydata{k} =  dlmread(filename);
end
mydata

% Now we can do calculations using the imported data.  For example, the
% element-by-element average of the 7 imported files is:
avg = zeros(1,length(mydata{1}));
for k = 1:length(mydata{1})
    tot = 0;
    for l = 1:length(mydata)
        tot = tot + mydata{l}(k);
    end
    avg(k) = tot/length(mydata);
end
avg

% Here's the average of each individual file:
avgB = zeros(1,length(mydata));
for k = 1:length(mydata)
    tot = 0;
    for l = 1:length(mydata{1})
        tot = tot + mydata{k}(l);
    end
    avgB(k) = tot/length(mydata{1});
end
avgB