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

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.addAnalogInputChannel(devId,'ai0','Voltage');
stimFlags.addAnalogInputChannel(devId,'ai1','Voltage');
stimFlags.Rate = 1000
stimFlags.DurationInSeconds = 5
fid = fopen('recording.csv','w');
lh = stimFlags.addlistener('DataAvailable',@(src,event)saveData(fid,event));
stimFlags.startBackground();
stimFlags.wait()
fclose('all');

 Respuesta aceptada

Mario Malic
Mario Malic el 25 de Sept. de 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.

5 comentarios

Thank you for asking this important question. This is how the saveData function looks like:
function saveData(fid,event)
time = event.TimeStamps;
data = event.Data;
plot(time,data)
fprintf(fid,'%f,%f,%f\n',time,data)
fclose(fid);
end
I would expect the whole recording of 5 seconds and 1000 datapoints per seconds to be the event and thus written into a single file. If, as you say, stimflag acquires only 10% of the rate by default, how can I adjust this property?
I need the total of 5000 data points to be written to one (or more) data files as the final output.
Try this first, as mentioned my the reply above.
fid = fopen('recording.csv','a');
In the code above, if I replace fid = fopen('recording.csv','w'); with fid = fopen('recording.csv','a'); I get an Excel output file with 100 data points, just like before. I never get the 5000 data points.
Mario Malic
Mario Malic el 27 de Sept. de 2023
Editada: Mario Malic el 27 de Sept. de 2023
Oh, I am so blind here, you don't need to close the file in the saveData function, remove this line.
fclose(fid);
Also, you can set breakpoints into that function and adjust the code so the function can write the data correctly into the file.
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Acquisition Toolbox Supported Hardware en Centro de ayuda y File Exchange.

Productos

Versión

R2016b

Preguntada:

el 22 de Sept. de 2023

Comentada:

el 9 de Oct. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by