Hello everybody,
I'm trying to acquire and store data from a CompactDAQ from National Instruments. The configuration of the device itselt and the visualization of the data is pretty straightforward, but I'm having trouble in store this data in a matrix that I will be able to access after the acquisition is over.
I'm connecting the device as follows:
s=daq.createSession('ni');
s.addAnalogInputChannel('cDAQ1Mod2', 0, 'voltage');
s.Rate=2000;
s.DurationInSeconds = 60;
to plot the data in real time I use the following lines of code:
lh = s.addlistener('DataAvailable', @plotData);
function plotData(src,event)
plot(event.TimeStamps, event.Data)
end
and then I start it in background (I need to acquire data in background in order to perform other tasks at the same time):
s.startBackground();
This seems to work fine for the real time problem, but I'm having trouble to define a callback function that will store all data acquired into a matrix or structure. I'm sure that this is a quite basic problem.
I want to thank you all in advance for your time.

댓글 수: 4

Function definitions are not permitted in this context. i'm getting this error while running the code s=daq.createSession('ni'); s.addAnalogInputChannel('cDAQ2Mod1', 0, 'voltage'); s.addAnalogInputChannel('cDAQ2Mod1', 1, 'voltage');
s.Rate=2000; s.DurationInSeconds = 60; lh = s.addlistener('DataAvailable', @plotData); function plotData(src,event) plot(event.TimeStamps, event.Data) end
You need to name the file nidaq6258.m, and the first line of the file must be
function nidaq6258
Hey Everyone,
Looks like I'm a few years late to the party. These solutions and comments have been very helpful, but I still have a problem. The while(~s.IsDone) loop doesn't appear to be working for me. When I run the code below, Matlab remains busy indefinitely while it's on the while(~s.IsDone) line. When I debug and step past it manually, the code that follows works. I'd like to use the command window while the session is running in the background which is why this is a problem.
Thanks for your help
J
function acquireData
clear global;
global data
s = daq.createSession('ni');
ch3 = addAnalogInputChannel(s,'cDAQ1Mod1', 'ai3', 'RTD');
ch3.RTDType = 'Pt3851';
ch3.RTDConfiguration = 'ThreeWire';
ch3.R0 = 100;
s.Rate = 100;
duration = input('Duration of Sampling Period?');
s.DurationInSeconds = duration;
time = (0.01:0.01:duration);
lh = s.addlistener('DataAvailable', @plotData);
startBackground(s);
while(~s.IsDone)
end
close(gcf);
plot(time,data);
delete(lh);
end
function plotData(src, event)
global tempData;
global data
if(isempty(tempData))
tempData = [];
end
plot(event.TimeStamps, event.Data)
tempData = [tempData;event.Data];
data = tempData;
end
Or put in a brief pause() in the while loop so that there is an opportunity for background code to run and events to be processed.

댓글을 달려면 로그인하십시오.

 채택된 답변

Chirag Gupta
Chirag Gupta 2011년 4월 26일

10 개 추천

Hi Nuno,
There are number of ways to achieve this:
1) Use some global data
function acquireData
global data
s = daq.createSession('ni');
s.addAnalogInputChannel('cDAQ1Mod1',0,'voltage')
s.Rate = 2000
s.DurationInSeconds = 5
lh = s.addlistener('DataAvailable',@plotData);
s.startBackground();
% Do something
while(~s.IsDone)
end
close(gcf);
plot(data); % plot global data
function plotData(src,event)
persistent tempData;
global data
if(isempty(tempData))
tempData = [];
end
plot(event.TimeStamps, event.Data)
tempData = [tempData;event.Data];
data = tempData;
end
2) Use NotifyWhenDataAvailableExceeds property (This decides when 'DataAvailable' is called. So if you don't want a live plot then this is suitable. Just set this to s.Rate * s.DurationInSeconds.
and have a callback for DataAvailable as:
function collectData(src,event)
time = event.TimeStamps;
data = event.Data;
plot(time,data)
save log.mat data time
disp(Background Acquisition complete);
end
This should save the data to a matfile.
3) Write Data to a file:
function acquireData
s = daq.createSession('ni');
s.addAnalogInputChannel('cDAQ1Mod1',0,'voltage')
s.Rate = 2000
s.DurationInSeconds = 5
fid = fopen('log.csv','w');
lh = s.addlistener('DataAvailable',@(src,event)saveData(fid,event));
s.startBackground();
while(~s.IsDone)
end
function saveData(fid,event)
time = event.TimeStamps;
data = event.Data;
plot(time,data)
fprintf(fid,'%f,%f\n',time,data)
end

댓글 수: 5

can u help me with the real time data plat s = daq.createSession('ni'); s.addAnalogInputChannel('cDAQ2Mod1', 0, 'Voltage'); s.addAnalogInputChannel('cDAQ2Mod1', 1, 'Voltage'); s.Rate = 2000 s.DurationInSeconds = 20; s [data,time] = s.startForeground; plot(time,data); xlabel('Time (secs)'); ylabel('Voltage') h = s.addlistener('DataAvailable', @plotData); function plotData(src,event) plot(event.TimeStamps, event.Data) end
mado
mado 2014년 2월 19일
can i use the first method with usb6008 to aquire data and save file ?
OJ27
OJ27 2015년 2월 13일
Hi,
I have been able to capture data in the background from a sensor while rotating it with a servo. However, I would like to apply an average filter for the collected points data. Could it be done in the function that is created for the listener? For example, I would like to average 10 samples before the rotating the servo to the next position.
Tim Helps
Tim Helps 2018년 3월 8일
Hi Chriag,
Thanks for these solutions!
However, when I used them I noticed one problem with your tempData variable - since it's persistent, the data will be there still after the acquireData function is completed. If the acquireData function is run again, the previous data will still be there and tempData (and thus data) will continue to grow.
I've fixed this in my code by making it a global rather than persistent variable, and using: clearvars -global at the start of the acquireData function.
If you have a better solution please let me know!
Many thanks, Tim
Hey,
I come from the future just to thank you for this. Just what I was looking for :)
Greetings

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Nuno Martins
Nuno Martins 2011년 4월 27일

1 개 추천

Thank you very much Chirag Gupta, it worked like a charm. You saved me a lot of time.

댓글 수: 2

avinash pabbireddy
avinash pabbireddy 2013년 11월 11일
편집: Walter Roberson 2018년 9월 26일
function nidaq9234
global data
s = daq.createSession('ni');
s.addAnalogInputChannel('cDAQ2Mod1',0,'voltage')
s.addAnalogInputChannel('cDAQ2Mod1', 1, 'Voltage');
s.Rate = 2000
s.DurationInSeconds = 20
lh = s.addlistener('DataAvailable',@plotData);
s.startBackground();
% Do something
while(~s.IsDone)
end
close(gcf);
plot(data); % plot global data
function plotData(src,event)
persistent tempData;
global data
if(isempty(tempData))
tempData = [];
end
plot(event.TimeStamps, event.Data)
tempData = [tempData;event.Data];
data = tempData;
end
i'm getting error while running the code
Error: File: nidaq9234.m Line: 26 Column: 1
The function "plotData" was closed
with an 'end', but at least one other function definition was not.
To avoid confusion when using nested functions,
it is illegal to use both conventions in the same file.
You need to add one more 'end' to this, either right after the plot(data) or at the very end. For your code structure, right after the plot(data) would make the most sense.

댓글을 달려면 로그인하십시오.

jenni
jenni 2013년 2월 18일

1 개 추천

Is it possible to add a timer function to this code? I want a function to be executed once in every 2 sec?
Csaba
Csaba 2011년 7월 18일

0 개 추천

Hi Chirag Gupta!
Do you know how to get the same result with a NI DAQ-6052E PCI acquisition card? I tried your code, but since I could not get any CompactDAQ device detected:
>> d=daq.getDevices
d =
No data acquisition devices available.
Click here for a list of known vendors.
Click here for troubleshooting tips.
??? Error: File: F20110718_AIAO_test_v15.m Line: 7 Column: 1
Function definitions are not permitted in this context.
the following line did not worked:
s.addAnalogInputChannel('cDAQ1Mod1',0,'voltage')
Maybe my device is not meant to work with sessions. It would be a shame, because the event detection would be very useful for me.
Indeed, my goal is to acquire 4 different signals on 4 different analog input through the NI BNC-2110 and the NI DAQ-6052E PCI card, plot them "live" as well as output "live" 3 different voltages (each would be a given function of the input voltages) on 3 different analog output.
Thank you very much! Kind regards,
Csaba

댓글 수: 1

Chirag Gupta
Chirag Gupta 2011년 7월 18일
As of MATLAB R2011a, only Compact DAQ devices are supported with the Session based architecture described above. You can try the R2011b prerelease (download from the MathWorks website) that supports other NI boards with the new architecture.
Otherwise, you can look at this demo:
"Continuous Acquisition Using Analog Input"

댓글을 달려면 로그인하십시오.

Dagmar Fraser
Dagmar Fraser 2011년 10월 18일

0 개 추천

OOh top work - been looking for something like this for literal years!
I had to change
% Do something
while(~s.IsDone)
end
to
s.IsDone % will report 0
s.wait() % rather than while
s.IsDone % will report 1

댓글 수: 1

Christos
Christos 2012년 3월 28일
hi! i would like to ask if it is possible to use the following code:
s.addAnalogInputChannel('Dev1','ctr0','EdgeCount'), in order to get the Edge count value from NI card usb 6221?
Thanks for your help

댓글을 달려면 로그인하십시오.

질문:

2011년 4월 26일

댓글:

2018년 9월 26일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by