logData too few inputs?

Hey Everyone,
I am trying to write some code with data acquisition that will stop background data collection when a signal is greater than 1.0, as well as log the data I am collecting. I think I am just missing something simple. As the error I am getting says the logData function doesn't have enough input arguments. However, I believe I am passing it all that it needs in my function (src,event,fid1). Suppressing the logData line fixes all issues. Please see my code below, and let me know if I can provide more details.
Best, Josh
clear all
clc
% Work around for hardware clocking issue
daq.reset;
daq.HardwareInfo.getInstance('DisableReferenceClockSynchronization',true);
% Creates a data acquisition session
s = daq.createSession('ni');
% Add an input channel for the load cell
ch1 = addAnalogInputChannel(s,'Dev2',0,'Voltage');
% Preps a file for data write
fid1 = fopen('log.bin','w');
% Sets the listener call rate to default (10 Hz)
s.IsNotifyWhenDataAvailableExceedsAuto = true;
% Configure a new listener to process data until a stop condition is met
lh = addlistener(s,'DataAvailable',@stopWhenExceedOneNm);
% Configure the session to acquire continuously
s.IsContinuous = true;
% Start acquiring data in the background
s.startBackground()
The function being called:
function stopWhenExceedOneNm(src,event,fid1)
if any(event.Data > 1.0)
disp('Event listener: Detected Voltage exceeds 1, stopping acquisition')
% Continuous acquisition stopped explicitly
src.stop()
plot(event.TimeStamps,event.Data)
else
plot(event.TimeStamps,event.Data);
% Log data
logData(src,event,fid1);
disp('Event listener: Continuing to acquire')
end
end

4 个评论

Josh - is the logData function something that you have written or is it a MATLAB toolbox function? What is the signature for this function? (i.e. if you open the logData.m file, what are the inputs and outputs to this function).
That's a good question, I thought that it was a MATLAB toolbox function, but looking again, I can't find any documentation for it. I got the information about using it as an anonymous function from the following link:
So, if logData isnt a toolbox function, I should just be able to create a matrix of data from event.TimeStamp and event.Data and write "data" using fwrite
In trying to get this to work though, I figured that my previous code was not passing variables to my function as I thought it would (so my code wasn't "seeing" fid1, thus giving me the error). If I try to create fid1 within the function though, the variable never shows in my workspace. Similarly, I added code to create an object "data" off of the event.TimeSTamp and event.Data, and when my function is called, these objects never show up in my workspace. As a result of fid1 and data never being created, the fwrite function gives an error of not having enough arguments. Any idea why my function as written would not be creating these objects? Below is my new code, any help is appreciated.
clear all
clc
% Creates a data acquisition session
s = daq.createSession('ni');
% Add an input channel for the load cell
ch1 = addAnalogInputChannel(s,'Dev2',0,'Voltage');
% Sets the listener call rate to default (10 Hz)
s.IsNotifyWhenDataAvailableExceedsAuto = true;
% Configure a new listener to process data until a stop condition is met
lh = addlistener(s,'DataAvailable',@stopWhenExceedOneNm);
% Configure the session to acquire continuously
s.IsContinuous = true;
% Start acquiring data in the background
s.startBackground()
The Function
function stopWhenExceedOneNm(src,event)
% Preps a file for data write
fid1 = fopen('log.bin','w');
if any(event.Data > 1.0)
disp('Event listener: Detected Voltage exceeds 1, stopping acquisition')
% Continuous acquisition stopped explicitly
src.stop()
plot(event.TimeStamps,event.Data)
else
plot(event.TimeStamps,event.Data);
% Log data
data = [event.TimeStamps,event.Data];
fwrite(fid1,data,'double');
disp('Event listener: Continuing to acquire')
end
end
Thanks for the FAQ link, I think that would be the way to execute what I want to do. After some playing around though, I think adding multiple listeners may be the way to go for "simplicity". Thanks again.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Data Acquisition Toolbox Supported Hardware 的更多信息

提问:

2015-5-7

评论:

2015-5-11

Community Treasure Hunt

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

Start Hunting!

Translated by