function data = GazeData(expt,stimulus,data,jtrial,trial);
if isfield(expt,'eyetracking') && expt.eyetracking==1
    
    REDdata='anything';
    jtime=0;
    while ~isempty(REDdata)
        REDdata = IVXreceive(expt.ivx);
        % Still seem to have some :s and ,s for some reason:
        REDdata=strrep(strrep(REDdata,':',''),',','');
        if length(REDdata)>6
            cmd = REDdata(1:6);
            REDdata = REDdata(8:end);
            if  strcmp(cmd,'ET_SPL') && ~isempty(REDdata)
                jtime=jtime+1;
                tmp=textscan(REDdata,'%f%f%f%f%f');
                eyetracking.REDtimeinms(jtime) = tmp{1};
                if isempty(tmp{4}) || isnan(tmp{4})
                    % presum,ably we have monoc data (or av of both)
                    eyetracking.x_IVX(jtime) = tmp{2};
                    eyetracking.y_IVX(jtime) = tmp{3};
                else
                    % we seem to have bincoular data
                    eyetracking.xL_IVX(jtime) = tmp{2};
                    eyetracking.xR_IVX(jtime) = tmp{3};
                    eyetracking.yL(jtime) = tmp{4};
                    eyetracking.yR_IVX(jtime) = tmp{5};
                end
            end
        end
    end % have now read all data forthcoming from eyetracker


    if isstruct(eyetracking)
        % did we get any data? If so:
        % Convert to time in sec since stim onset
        eyetracking.time_SecsSinceStimOnset = (eyetracking.REDtimeinms-eyetracking.REDtimeinms(1))/1000;
        % extract mean eye piostion if necessary:
        if ~isfield(eyetracking,'x')
            eyetracking.x_IVX = (eyetracking.xL_IVX+eyetracking.xR_IVX)/2;
            eyetracking.y_IVX = (eyetracking.yL+eyetracking.yR_IVX)/2;
        end
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Remove rubbish!       
        %%%%%%%%%%%%%%%%%%%%%%%%%%%
        % Sometimes the eyetracker outputs (0,0) for reasons we don't
        % understand, presumably because it lost your gaze position for a
        % while. I am going to wipe these from the record!
        jj = find( [eyetracking.x_IVX]~=0 | [eyetracking.y_IVX]~=0);
        eyetracking.x_IVX = eyetracking.x_IVX(jj);
        eyetracking.y_IVX = eyetracking.y_IVX(jj);
        eyetracking.REDtimeinms = eyetracking.REDtimeinms(jj);
        eyetracking.time_SecsSinceStimOnset =  eyetracking.time_SecsSinceStimOnset(jj);
        
        % Now, x_IVX is the coordinates of gaze on the screen area
        % available to the IVX. In this version of the code, this is not
        % necessarily the whole screen used by the PTB window. So, correct
        % back to PTB coordinates.
        eyetracking.x_PTB = eyetracking.x_IVX + expt.visstimscreenrect(1);
        eyetracking.y_PTB = eyetracking.y_IVX + expt.visstimscreenrect(2);
        
        % Now go through and classify gaze position moment by moment
        % Initialise time fields to zero:
        eyetracking.secs_lookingoffstimarea = 0;
        switch expt.ExptType
            case 'Patchwork'
                eyetracking.secs_lookingatobject = zeros(1,expt.nsquares);
            case 'Squares'
                eyetracking.secs_lookingatobject = zeros(1,expt.nFG+1);
        end
        totaltime=0;
        for jt=1:length(eyetracking.REDtimeinms)
            x = eyetracking.x_PTB(jt);
            y = eyetracking.y_PTB(jt);
            % secs since last position
            if jt>1
                dt = eyetracking.time_SecsSinceStimOnset(jt) - eyetracking.time_SecsSinceStimOnset(jt-1);
            else
                dt = 1/expt.ivx.samplerate;
            end
            eyetracking.secssincelastpos(jt) = dt;
            
            totaltime = totaltime+dt;
            if x<expt.visstimscreenrect(1) || x>expt.visstimscreenrect(3) || y<expt.visstimscreenrect(2) || y>expt.visstimscreenrect(4)
                eyetracking.classifiedgazeposition(jt) = NaN; % outside visual stim area
                eyetracking.secs_lookingoffstimarea = eyetracking.secs_lookingoffstimarea + dt;
            else
                % they were looking at the stimarea; classify where:
                switch expt.ExptType
                    case { 'Squares' , 'Patchwork'}
                        jposgaze=expt.nsquares+1; % start off by assuming they were looking at the bg
                        for jsq=1:expt.nsquares
                            if expt.squarepos{jsq}(1)<=x && x<expt.squarepos{jsq}(3) && expt.squarepos{jsq}(2)<=y && y<expt.squarepos{jsq}(4)
                                jposgaze=jsq;
                            end
                        end
                        eyetracking.classifiedgazeposition(jt) = jposgaze;
                        if jposgaze<=length(eyetracking.secs_lookingatobject)
                            eyetracking.secs_lookingatobject(jposgaze) = eyetracking.secs_lookingatobject(jposgaze) + dt;
                        end
                        
                    case 'singletarget'
                        % Here there is only 1 target. Pos2=background, Pos1=object
                        jposgaze = 2;
                        if trial.squarepos(1)<x && x<trial.squarepos(3) && trial.squarepos(2)<y && y<trial.squarepos(4)
                            jposgaze=1;
                        end
                        eyetracking.classifiedgazeposition(jt) = jposgaze;
                        eyetracking.secs_lookingatobject(jpos) = eyetracking.secs_lookingatobject(jpos) + dt;
                        
                end % finish classifying where they were looking
            end
        end % finsih going through time
        
        %%%%%%%%%%%%%%%%%%%%%%%%%
        % Plot eyetracking data:
        %%%%%%%%%%%%%%%%%%%%%%%%%
        figure(expt.ivx.fig)
        hold off
        imagesc([1:expt.nx],[1:expt.ny],stimulus);
        set(gca,'position',[0 0 1 1],'ydir','rev')
        axis equal off
        hold on
        
        % Draw a circle round each point whose radius indicates the amount
        % of time since the last point collected!
        % if they stayed looking at 1 point for the whole trial, the radius
        % would be 300 pixels
        for jt=1:length(eyetracking.x_PTB)
            rad = eyetracking.secssincelastpos(jt)/totaltime * 300;
            if rad>0
                rectangle('pos',[eyetracking.x_PTB(jt)-rad eyetracking.y_PTB(jt)-rad 2*rad 2*rad],'curvature',[1 1],'edgecol','k','facecol','none')
            end
        end

        if isfield(data,'touch_xpix')
            % Mark where they pressed:
            plot(data.touch_xpix,data.touch_ypix,'w*','markersize',12,'linew',2)
        end
        
        % Mark gaze position:
        plot(eyetracking.x_PTB,eyetracking.y_PTB,'k.-')
        hold on
        plot(eyetracking.x_PTB,eyetracking.y_PTB,'c.:')
        % Summary of output
        X=0.1;Y=0.9;
        text(X,Y,sprintf('Total time: %5.2f s',totaltime),'units','norm');
        Y=Y-0.05;
        text(X,Y,sprintf('Off stim-area: %5.2f s',eyetracking.secs_lookingoffstimarea),'units','norm');
        for jp=1:length(eyetracking.secs_lookingatobject)
            Y = Y-0.05;
            text(X,Y,sprintf('   Object %d: %s: %5.2f s',jp,expt.stimulusnames{trial.colindices(jp)},eyetracking.secs_lookingatobject(jp)),'units','norm','col','y');
        end
        drawnow
        % Save this figure to a jpg:
        jpgname = strrep(expt.filename,'.mat','');
        jpgname = sprintf('%s_%d.jpg',jpgname,jtrial);
        print(expt.ivx.fig,jpgname,'-djpeg');
        
    end % end checking we got eye-tracking data
    
    data.eyetracking=eyetracking;
end