Search code examples
matlabpsychtoolbox

How to plot a "moving" graph for real time values along the x-axis (using psychtoolbox)?


I am writing a code for a real time experiment using psychtoolbox to present the stimulus. In my experiment, I need to show the subject a graph that indicates his performance. I have plotted the graph using this simple code:

        % Draw the graph
        figure('visible','off','color',[0 0 0]);
        pcolor([0 Num_timepoint+2],[-10 0],ones(2,2));
        hold on;
        pcolor([0 Num_timepoint+2],[0 10],2*ones(2,2));
        colormap([79 167 255;255 187 221]/256);      
        plot(1:subloop,value0,'*-',...
        'color',[0,0,0],...
        'LineWidth',1,...
        'MarkerSize',5,...
        'MarkerEdgeColor','k',...
        'MarkerFaceColor',[0.5,0.5,0.5]);
        axis([0,Num_timepoint+2,-10,10]);
        saveas(gcf,'line_chart.png');   %save it
        close(figure);
        line_chart=imread('line_chart.png');   %read it
        resized_plot=imresize(line_chart,0.5);
        imageSize = size(resized_plot);
        [imageHeight,imageWidth,colorChannels]=size(resized_plot);
        bottomRect = [xCenter-imageWidth/1.5, yCenter+gapdown, xCenter+imageWidth/1.5, yCenter+gapdown+imageHeight];
        imageDisplay=Screen('MakeTexture', win0, resized_plot);
        Screen('DrawTexture', win0, imageDisplay, [], bottomRect);

Unfortunately, this simple code is very slow. In addition, I couldn't make the graph moving along x axis, as soon as the new value comes.

Any help would be Awesome. Thanks in advance for your efforts.


Solution

  • Why are you saving the figure and redisplaying as an image? Maybe I'm missing something but you should be able to accomplish what you need by updating the existing plot with the fresh data using the handles properties of the plot:

     .... First time through we need the initial plot ....
     % Set up figure with colormaps and such but leave as 'visible','on'
     hPlot = plot(plot(1:subloop,value0,'*-',...
        'color',[0,0,0],...
        'LineWidth',1,...
        'MarkerSize',5,...
        'MarkerEdgeColor','k',...
        'MarkerFaceColor',[0.5,0.5,0.5]);
     hAxes = gca;
    
    
     .... Loop over your real time updates ....
     % Assuming value0 has subject's results from t=0 to t=now 
     hPlot.XData = value0; hPlot.YData = [1:subloop];   
     hAxes.XLim = [0 numTimePoints+2];
     hAxes.YLim = [-10 10]
    
     .... Continue test and update value0 ....
    

    I think that should keep your plots current without having to save the figure as image to file then reopen the image to display to subject.