Stream LabChart to Matlab

Join discussions about the use of our software products in Research.
Posts: 2
Joined: Sun Jan 28, 2018 4:44 pm

Stream LabChart to Matlab

Post by Dxc536 » Sun Jan 28, 2018 8:37 pm


I've found a bunch of post for this topic but I think I still need help.

Essentially, I would like to continuous stream data from labchart into Matlab; I have a matlab script that has subject's track sine wave using their smoothed EMG.

I've modified the Matlab_Sample and the LCCallback and I am able to get it to work by creating global variable from the chanSamples; however, I have a couple follow up questions because I don't think what I've done is optimal.

First, here is the Matlab_Sample code:

Code: Select all

%This script demonstrates live data streaming from LabChart to MATLAB.

%1) Open a document in LabChart
%2) Run this script in MATLAB
%3) When LabChart starts sampling, the latest data from channels 1 and 2 are 
%plotted in MATLAB (while sampling).
%4) If you stop sampling and make a selection in LabChart, then MATLAB will plot the

global gLCApp;
global gChans; %Array of channel numbers specifying channels to retrieve sampling data from

%First a reference to a LabChart instance (gLCApp) is obtained, either by attaching to 
%an already running LabChart, or starting LabChart.
    gLCApp = actxGetRunningServer('ADIChart.Application');
catch err
    error('Please start LabChart before running this script.');
if (isempty(gLCApp.ActiveDocument))
    error('Please open a document in LabChart before running this script.');

global Channel1 Channel2;
Channel1 = 1;
Channel2 = 2;

gChans = [Channel1 Channel2]; % collect sampling data from channels 1 and 2.

RegisterLCEvents(gLCApp.ActiveDocument); % hook up the OnSelectionChange event and the sampling events such as OnNewSamples and OnBlockStart.

and here is the LCCallback code:

Code: Select all

function a = LCCallBacks(str)
%Example event handlers for some of the events generated by a LabChart document.
a = str2func(str);

function OnSelectionChange(varargin)
%Example event handler. Called when the selection changes in the LabChart
%This example gets the selected data as a (2D) matrix with each column
%corresponding to a channel and each row corresponding to a time point.
global gLCDoc;
disp('OnSelectionChange called')
kArrayOfDoubles = 1;  % GetSelectedData returns a matrix of doubles, not variants (cells)
kAllSelectedChannels = -1; % GetSelectedData returns all selected channels, rather just the specified channel.
data = gLCDoc.GetSelectedData(kArrayOfDoubles, kAllSelectedChannels);
subplot(1,1,1); %no subplots

function OnBlockStart(varargin)
%Example event handler called when sampling and a new block is about to be
%added to the document. This is called *before* the new block has been
%added, so gLCDoc.NumberOfRecords does not yet include the new block.
%Initialise the global variables used by OnNewSamples().
global gLCDoc;
global gLatestBlock;
global gBlockSecsPerTick;
global gLatestTickInBlock;
global gChans;
global gChansData;
global gT;
disp('OnBlockStart called')
gLatestBlock = gLCDoc.NumberOfRecords
gBlockSecsPerTick = gLCDoc.GetRecordSecsPerTick(gLatestBlock)
gNChanSamples = 0;
gT = [];   %time (from start of block)
gLatestTickInBlock = 0;

%gChansData is a cell array because LabChart channels are not necessarily all the same
%length while sampling, since the data can arrive in each channel with
%different delays.
gChansData = cell(size(gChans)); 

function OnNewSamples(varargin)
%Example event handler called when sampling, whenever new samples might be 
%available, typically 20 times a second.
%This example gets any new samples from channel gChan, appends them to
%the gChan1Data vector, then plots the latest 5000 samples.
global gLCDoc;
global gLatestBlock;
global gBlockSecsPerTick;
global gLatestTickInBlock;
global gChans;
global gChansData;
global gT;
global Channel1 Channel2;
global chanSamples1
global Chan1Val
global chanSamples2
global Chan2Val
%disp('OnNewSamples called')
gLatestTickInBlock = gLatestTickInBlock+double(varargin{3});

chanSamples1 = gLCDoc.GetChannelData (1, Channel1, gLatestBlock+1, length(gChansData{Channel1})+1, -1);
chanSamples2 = gLCDoc.GetChannelData (1, Channel2, gLatestBlock+1, length(gChansData{Channel2})+1, -1);
gChansData{Channel1} = [gChansData{Channel1}; chanSamples1'];
gChansData{Channel2} = [gChansData{Channel2}; chanSamples2'];

if numel(chanSamples2>20)
    Chan2Val = mean(chanSamples2);
    Chan1Val = mean(chanSamples1);

function OnBlockFinish(varargin)
global gLCDoc;
global gLatestBlock;
global gBlockSecsPerTick;
global gLatestTickInBlock;
global gChans;
global gChansData;
global gT;
disp('OnBlockFinish called')
%This would be a good place to save the gChansData and gT for the completed
%block, if needed.

Essentially I am using the Chan2Val and Chan1Val as global variables in another script.

Question 1) It appears when I have it set up this way Matlab is sampling at 5Hz rather than 20Hz...I'm assuming I deleted something I shouldn't have or have some option in the LCCallback, but I really don't know how to fix this.

Question 2) How can I pull from channels other than 1 and 2? When I change the gChans [3,4], I get a lot of continuous errors "Error firing event 'OnNewSamples' to 'OnNewSamples'

Question 3) in line with question 2, can I call from more than 2 channels at one time? Basically can I have a Chan3Val and Chan4Val that I can use globally along with Chan2Val and Chan1Val?