%scantext060428.m
%This script scans a text file exported from the Victor3 plate reader and
%creates a cell array C that holds a text or number value for each element
%in the file. C is then converted to a structure array s. The 'HH:MM:SS'
%times in Column 5 (t1) and Column 7 (t2) are converted to minutes.

%Rows and columns are selected for analysis. s1 (umbelliferone) and Abs (570) data matrices and
%corresponding time matrices are assembled and plotted.
%    01  02	 03   04  05  06  07  08 09  10  11  12
% A |   |   |   |   |   |   |   |   |   |   |   |   |
% -----------------------------------------------------
% B  etc Victor data is stored in one column, row wise A01-A12, B01-B12 etc

clear all

ls *.txt

name=input('Enter Filename from list above:', 's')

fid = fopen(name)
    %Data fields are:
    %1Plate 2Repeat 3Well 4Type 5Time 6A405 7Time 8A570
    C = textscan(fid, '%d %d %s %s %s %f %s %f', 'headerLines', 1);
fclose(fid);
fields = {'Plate' 'Repeat' 'Well' 'Type' 't1' 's1' 't2' 's2'};
s = cell2struct(C, fields , 2);
%times=datenum(C{5},  'hh:mm:ss.ss')
dv1 = datevec(s.t1, 'HH:MM:SS');
t1=dv1(:,6)/60 + dv1(:,5) + dv1(:,4)*60 ; %converts to minutes
% dv2 = datevec(s.t2, 'HH:MM:SS');
% t2=dv2(:,6)/60 + dv2(:,5) + dv2(:,4)*60 ; %converts to minutes

b = unique(s.Well); %finds unique wells
%plots=input(['There are ', b, 'unique wells, how many plots?:'])

n = size(b, 1);     %number of unique wells
n2 = max(s.Repeat);  %number of repeats (also could do -- size(s.Well, 1) / n

 t1r=reshape(t1, n, n2);  % time (x) 1 row for each well, 1 column for each repeat
 s1r=reshape(s.s1, n, n2); % signal(y) ""
 l2s1r = log2 (s1r);

% t2r=reshape(t2, n, n2);
% s2r=reshape(s.s2, n, n2);

for j=1:n
    lp = polyfit(t1r(j,:), l2s1r(j,:),1);   %fit log2Abs vs time to 1st order polynomial (y=mx+b)
                                            %lp = [m b] slope and intercept
    logline = polyval (lp, t1r(j,:));    %generates a theoretical point for each time
    fitline = pow2(logline) ;        %performs the operation 2^x, converts back to original
    pm(j,:) = lp;                   %parameter matrix; slope c1, intcpt c2
    lm(j,:) = logline;              %fit lines matrix
end

dtime = 1./pm(:,1)         % reciprocal of doubling rate is doubling time
dt=num2str(dtime,'%4.0f') ;   % converts to string for title

%
% s=num2str(pc,'%4.2f');   %slopes x1000  as strings fixed format
 ll= strcat(b, dt);         %catenate Well ID & slope for legend labels

figure	%first 60 wells - 8 plots of 6 wells/plot
f=1     %first data set to plot
for i=1:6       %i subplots
    subplot (3,2,i);
    l = f + 5;   %last data set to plot, 6 lines per plot
%     plot (t1r(f:l,:)', s1r(f:l,:)');
    plot (t1r(f:l,:)', s1r(f:l,:)', 'o', t1r(f:l,:)', s1r(f:l,:)', '--');

    %axis ([0 150 0.08 0.25 ]);%   AXIS([XMIN XMAX YMIN YMAX])
    legend (ll(f:l), 'Location','EastOutside');  %takes the appropriate well names
    title([name, date])
    f = f + 6;
end
    %Now f should be 1 + (6*8) = 49
set(gcf, 'PaperPosition', [0.5 0.5 7 10]);

figname = strrep(name, '.txt', '.pdf')
print ('-dpdf', figname)
Error using ==> input
Cannot call INPUT from EVALC.