classdef pamtraceAv

    properties (SetAccess = public)
        filename
        pamtraces

    end

    methods
        function trace = pamtraceAv(filename) %constructor
            if nargin < 1
                error('provide filename')
            end
            trace.filename=filename;
            %  trace.basefilename  = regexp(filename, '(.*)\.asc', 'tokens');
            % trace.basefilename=filename;%trace.basefilename{1}{1};
            %search current directory for all files that contain the base
            %filename in ther title

            g=dir;
            kk=1;
            for k=1:length(g)
                disp(g(k).name)
                ismatch1=regexp(g(k).name,[ 'wt_' filename '_\d(.*).asc']);

                if ismatch1
                    fullfilename=regexp(g(k).name, ['(' filename '.*)\.'], 'tokens');
                    if ~any(strfind(fullfilename{1}{1}, 'params'));
                        trace.pamtraces{kk}=pamtrace1([fullfilename{1}{1} '.asc']);
                        kk=kk+1;
                    end
                end
            end

        end%pamtraceAv


        function plotdiff(pamtraceAv, fnum, plotstyle)
            if nargin<3
                plotstyle='k'
            end
            p=pamtraceAv
            [mwt swt pulsetimes]=getnpqmean(p,'wt');
            [mnpq4 snpq4]=getnpqmean(p,'npq4');

            m=mwt-mnpq4;
            s=sqrt(swt.^2+snpq4.^2);
            figure(fnum)
            set(gca, 'fontsize', 22)
            hold on
            errorbar((pulsetimes), m,s, plotstyle, 'linewidth', 1.5)
          %  plot(pulsetimes, zeros(size(pulsetimes)), 'g')
          xlabel('time(seconds)')
            grid off
            box on
        end


        function plotnpq(pamtraceAv, fnum)
            if nargin==1
                fnum=figure
            end
            p=pamtraceAv.pamtraces;
            colors='rgbcmykrgb';
            for k=1:length(p)
                h=p{k}

                h.plotNPQ('wt', fnum, colors(k))
            end
             xlabel('time(seconds)')


        end


        function plotnpq4(p, fnum)
            g=fields(p);
            colors='rgbcmykrgb';

            for k=1:length(g)
                h=p.(g{k})

                h.plotNPQ('npq4', fnum, colors(k))
            end


        end

        function plotnpqmean(pamtraceAv, mutant,fnum, plotstyle)

            if nargin<4
                plotstyle='k';
            end
            if nargin<3
                fnum=figure;
            end

            [m s pulsetimes]=getnpqmean(pamtraceAv, mutant)

            figure(fnum)
            hold on
            set(gca, 'fontsize', 22)
             xlabel('time(seconds)')
            errorbar(pulsetimes, m,s, plotstyle)
            grid off
            box on

        end


        function [ m s pulsetimes]=getnpqmean(pamtraceAv, mutant)
            p=pamtraceAv.pamtraces;


            for k=1:length(p)
                h=p{k}
                a=fields(h.params)
                if any(strcmp(mutant, a))
                    r=h.params.(mutant);
                    n(k,:)=r.npq;
                end
            end
            pulsetimes=r.pulsetimes


            m=mean(n,1);
            s=std(n,1);

        end


    end % methods


end %classdef