Log Analog Input Data to a File Using NI Devices and Continously Plot Data

I am trying to both log analog dat and continuously plot the data so I can monitor it as my experiment runs. I am using a NI DAQ. Here is my code:
daq.getDevices;
s = daq.createSession('ni');
s.Rate = 1.8;
s.DurationInSeconds = 60;
ch1 = addAnalogInputChannel(s,'cDAQ1Mod1','ai0','Thermocouple');
ch2 = addAnalogInputChannel(s,'cDAQ1Mod1','ai1','Thermocouple');
ch3 = addAnalogInputChannel(s,'cDAQ1Mod1','ai2','Thermocouple');
ch4 = addAnalogInputChannel(s,'cDAQ1Mod1','ai3','Thermocouple');
ch5 = addAnalogInputChannel(s,'cDAQ1Mod1','ai4','Thermocouple');
ch6 = addAnalogInputChannel(s,'cDAQ1Mod1','ai5','Thermocouple');
ch7 = addAnalogInputChannel(s,'cDAQ1Mod1','ai6','Thermocouple');
ch8 = addAnalogInputChannel(s,'cDAQ1Mod1','ai7','Thermocouple');
ch1.ThermocoupleType = 'T';
ch2.ThermocoupleType = 'T';
ch3.ThermocoupleType = 'T';
ch4.ThermocoupleType = 'T';
ch5.ThermocoupleType = 'T';
ch6.ThermocoupleType = 'T';
ch7.ThermocoupleType = 'T';
ch8.ThermocoupleType = 'T';
fid1 = fopen('log.bin','w');
lh = addlistener(s,'DataAvailable', @plotData);
lh1 = addlistener(s,'DataAvailable',@(src, event)logData(src, event, fid1));
s
s.IsContinuous = true;
s.startBackground();
pause(60);
s.stop
fclose(fid1);
fid2 = fopen('log.bin','r');
[data,count] = fread(fid2,[9,inf],'double');
fclose(fid2);
My two functions are as follows:
function plotData(src,event)
plot(event.TimeStamps, event.Data)
legend('Outside Sensor 1','Middle Sensor 1','Middle Sensor 2','Middle Sensor 4', ...
'External Sensor 2','Outside Sensor 2','Middle Sensor 3','External Sensor 1')
end
function logData(src, evt, fid1)
data = [evt.TimeStamps, evt.Data];
fwrite(fid1,data,'double');
end
When I use s.NotifyWhenDataAvailableExceeds = 20 I can get the listener function to plot the data at discrete time steps, however, the log file is jumbled. When I use s.Iscontinous = true I can get the data to log correctly, however, I cannot see any data points on my plot. Any idea how to do both at the same time. Meaning how to continuously update the plot and then after it is done to log everything in a data array?

Antworten (0)

Gefragt:

am 23 Jun. 2016

Bearbeitet:

am 23 Jun. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by