Skip to content

Online Stream Example

Read a streaming data store into MATLAB from Tank server during a recording

Download M File

Download Experiment File

Housekeeping

Clear workspace and close existing figures. Add SDK directories to MATLAB path.

close all; clc;
[MAINEXAMPLEPATH,name,ext] = fileparts(cd); % \TDTMatlabSDK\Examples
[SDKPATH,name,ext] = fileparts(MAINEXAMPLEPATH); % \TDTMatlabSDK
addpath(genpath(SDKPATH));

Setup

EVENT = 'EEG1';
t = SynapseLive('MODE', 'Preview', 'EXPERIMENT', 'OnlineStreamDemo');
t.VERBOSE = false;
Connected to TANK: F:\Tanks\OnlineStreamDemo-220120-091932, BLOCK: TempBlk
Waiting for initial data.............done

Main Loop

first_pass = true;
while 1

    % slow it down
    pause(1)

    % get the most recent data, exit loop if the block has stopped.
    if isempty(t.update)
        break
    end

    % grab the latest events
    r = t.get_data(EVENT);
    if isstruct(r)

        % plot them
        ts = linspace(t.T1, t.T2, max(size(r.data))-1);
        plot(ts, r.data(:,1:end-1)')
        title(EVENT)
        xlabel('Time, s')
        ylabel('V')
        axis tight

        % force the plots to update
        try
            snapnow
        catch
            drawnow
        end
    end

    % for publishing, end early
    if t.T2 > 30
        t.SYN.setModeStr('Idle'); % set to idle mode
        break
    end
end

Runtime Output