Clear Filters
Clear Filters

How do I register/record analog input using a DAQ device?

6 views (last 30 days)
I would like to record the stimulator output with a DAQ device to verify the correctness of the stimulation sequences.
With the below code I tried to collect 1000 datapoints per second for 5 seconds which should result in a total of 5000 datapoints. These datapoints should be saved in a recording.csv file. However, I never get more than 100 datapoints. Why is that and how can I succeed in collecting data for a specific amount of time (in seconds) at a high rate (min. 1000)?
I am using MATLAB 2016b.
DAQ_device = daq.getDevices;
devId = DAQ_device.ID;
stimFlags = daq.createSession('ni');
stimFlags.Rate = 1000
stimFlags.DurationInSeconds = 5
fid = fopen('recording.csv','w');
lh = stimFlags.addlistener('DataAvailable',@(src,event)saveData(fid,event));

Accepted Answer

Mario Malic
Mario Malic on 25 Sep 2023
How does your saveData function look like? Most likely, you need to open your file in append mode. Every time you write, data just gets overwritten and you end up with incomplete data.
Session object that you are using will try to acquire 10% of the Rate (by default) in each reading, which you can change with specific property which is I believe irrelevant in your case.
You don't need to use stimFlags.wait() unless you are doing something with signal immediately after the acquisition is completed.
Mario Malic
Mario Malic on 27 Sep 2023
Edited: Mario Malic on 27 Sep 2023
Oh, I am so blind here, you don't need to close the file in the saveData function, remove this line.
Also, you can set breakpoints into that function and adjust the code so the function can write the data correctly into the file.
Magdalena on 9 Oct 2023
Thank you for your valuable advice! Deleting the fclose(fid) statement from the saveData function solved the problem and I received a .csv file containing 5000 datapoints.

Sign in to comment.

More Answers (0)


Find more on Data Acquisition Toolbox Supported Hardware in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!