 data(:,sel);   
        sel_data = sel_data(2:end,:);

        c = 0;
        
        if fit_offset
            initial_guess = [1 1];
        else
            initial_guess = 1;
        end
        
        [o,f] = fminsearch(@objt,initial_guess);

        counts_per_photon(i) = o(1);
        if fit_offset
            offset(i) = o(2);
        else
            offset(i) = 0;
        end
        waitbar(i/N,h);
    end
    close(h);

    st = ['Counts per photon = ' num2str(mean(cou