Skip to content

Instantly share code, notes, and snippets.

@jlblancoc
Created March 7, 2019 10:36
Show Gist options
  • Save jlblancoc/e315ae2feb91c316b3213b67ae9bf76f to your computer and use it in GitHub Desktop.
Save jlblancoc/e315ae2feb91c316b3213b67ae9bf76f to your computer and use it in GitHub Desktop.
Example: capture analog streams with NationInstrument DAQ from MATLAB
function [] = main()
close all;
% Make sure:
disp('Searching devices...');
devs=daq.getDevices();
if (length(devs.Vendor)~=1),
error('Is the USB DAQ connected?');
end
dev=devs(1);
fprintf('Connected to: %s\n', dev.Description);
% Create session:
s = daq.createSession ('ni');
[ch1,idx1] = s.addAnalogInputChannel(dev.ID, 'ai1', 'Voltage');
[ch2,idx2] = s.addAnalogInputChannel(dev.ID, 'ai9', 'Voltage');
ch1.TerminalConfig='SingleEnded';
ch2.TerminalConfig='SingleEnded';
% Capture:
s.IsContinuous = true;
s.Rate = 1000;
s.prepare();
global GRAB;
GRAB=[];
disp('Push any KEY to START...');
pause;
%[data,timeStamps,triggerTime] = s.startForeground();
lh = s.addlistener('DataAvailable', @plotData);
s.startBackground();
disp('Push any KEY to STOP...');
pause;
s.stop();
delete (lh);
FIL=sprintf('GRAB_%s.txt', datestr(now(),'yyyy_mm_dd_HH_MM_SS') ) ;
fprintf('Saving to: %s\n',FIL);
save(FIL,'GRAB', '-ascii');
end
function plotData(src,event)
global GRAB;
GRAB=[GRAB;event.TimeStamps event.Data];
plot(GRAB(1:150:end,1),GRAB(1:150:end,2:end));
title(sprintf('Valor: AL=%f F=%f',event.Data(end,1),event.Data(end,1)));
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment