MATLAB scrolling plot

2019-07-15 01:32发布

问题:

I have an EEG data base that I would like to plot. The database is a 19*1000*134 matrix, with:

  • 19 being the number of channel. On a first approach, I'm working with only one channel.
  • 1000 the size of a sample (1000 points for a sampling rate of 500 Hz, i.e. 2 sec of data)
  • 134 the number of epochs (number of different 2 second experience)

The idea is to plot epoch n right after epoch n-1 on the same graph. The (X,Y) matrix used to plot this has a 134 000 * not_much size, and I would like to be able to scroll horizontally on the plot, to see individually each epoch.

My code right now, plotting only one channel:

fs = s_EEG.sampling_rate;
[channel, length, nb_epoch] = size(s_EEG.data)

display(s_EEG.data, fs, length, channel, nb_epoch)

function display(data, fs, length, channel, nb_epoch)
    figure("Name", "Epoch display")

    for j = 1:nb_epoch
        time = 0.002+(2*j-2):1/fs:2*j;
        epoch = data(1,:,j);
        plot(time, epoch)
        hold on
    end
    hold off

end

Current output:

I'm completely new to Matlab, and I don't use it well yet, but I would like to find a way to see on the same graph, individually, and at a correct visualization scale, all of my 134 epochs (one color = one epoch above).

Thanks !

回答1:

This is very similar to something I already had so I tweaked it a bit for you. Basically pass plotData your data matrix. It will plot each of your items sequentially as you already have now.

Pressing the slider will change your x-limits so that you will step through 1 element (epochs) at a time. Clicking in the area will advance 2-epochs at a time. It currently just displays what you currently viewed "epoch" # is at the command line disp(['Current Epoch: ' num2str(viewI)]) However, it should be easy for you to redirect that to a text box on the figure to more readily know which you are viewing ... besides mentally dividing the x-limits by 2.

Use the list box to switch to a new channel which will reset the plot & x-limits.

Call it like this at the command line.

>> plotData( data )

CODE: Save everything below as plotData.m

function plotData( data )
% data = rand(19,1000,134);
f = figure('Units','Normalized','Position',[0.25 0.25 0.5 0.5]);
a =   axes('Units','Normalized','Position',[0.05 0.15, 0.75 0.75]);
s =   uicontrol(f, 'Style','Slider', 'Units','Normalized','Position',[0.05 0.025, 0.75 0.05],...
                   'Min',1,'Max',size(data,3),'Value',1, 'Callback',{@sliderChange,a} );
l =   uicontrol(f, 'Style','listbox','Units','Normalized','Position',[0.85 0.15, 0.1, 0.75],...
                   'String',cellstr(num2str([1:size(data,1)]')),'Callback',{@changeChannel,a,s,data} );

stepSize = 1/(s.Max - s.Min);
s.SliderStep = [stepSize 2*stepSize];               
changeChannel(l,[],a,s,data)

function changeChannel(l,evtData,a,s,data)
cla(a);
chanNum = str2double(l.String{l.Value});
sR = 500;  %500Hz
tempData = reshape(data(chanNum,:,:),[],size(data,3)); %Reshape each epoch into a column
tempTime = [0:1/sR:(size(data,2)-1)/sR]' + (0:1:size(data,3)-1)*2; %Build time array
plot(a,tempTime,tempData) %plot all the lines
s.Value = 1; %Rest Slider Position

function sliderChange(s,evtData,a)
viewI = round(s.Value);
disp(['Current Epoch: ' num2str(viewI)])
xlim(a,[(viewI-1)*2 viewI*2] + [-.1 .1])

Note: This uses implicit expansion so you need Matlab 2016b or higher.