0

I am collecting data using the Data Acquisition Toolbox. The data is collected in the background. I am collecting the data from 8 thermocouples and am able to call my listener to continually update my plot. However, I also need to keep a file, array, or some sort of log of the collected data. 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.NotifyWhenDataAvailableExceeds = 20;
s.startBackground();

fclose(fid1);
fid2 = fopen('log.bin','r');
[data,count] = fread(fid2,[8,inf],'double');
fclose(fid2);

The function plotData is:

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

The function logData is:

function logData(src, evt, fid)
    data = [evt.TimeStamps, evt.Data]' ;
    fwrite(fid,data,'double');
end

Any idea of how to keep a continuously update log of the data as it is collected while at the same time updating a log file of all the data points collectected?

amkas90
  • 139
  • 1
  • 9

1 Answers1

1

MATLAB doesn't store data unless you tell it to. In this case you should preallocate a variable, structure or file to write your data to.

See this MathWorks link for help creating a and writing to a log file: Acquire Continuous and Background Data Using NI Devices

K.G
  • 71
  • 6
  • I tried to add code from this Mathworks link - see the updated code above. When I do so - the continuous plotting stops working and to my knowledge I do not log any data. – amkas90 Jun 23 '16 at 16:41
  • Try adding a second listener of a different name. Listener1 can stay the same: lh = addlistener(s,'DataAvailable', @plotData); fid1=fid1 = fopen('log.bin','w'); Listener2 should log the data: lh2 = addlistener(s,'DataAvailable',@(src, event)logData(src, event, fid1)); where logData is: function logData(src, evt, fid) data = [evt.TimeStamps, evt.Data]' ; fwrite(fid1,data,'double'); end – K.G Jun 23 '16 at 16:49
  • Thanks I have updated the code above as you have suggested. I now get this error: "Warning: Error occurred while executing the listener callback for event DataAvailable defined for class daq.ni.Session: Error using fwrite Invalid file identifier. Use fopen to generate a valid file identifier. Error in logData (line 3) fwrite(fid,data,'double');" Any thoughts? – amkas90 Jun 23 '16 at 17:09
  • Do you actually have the file log.bin created? You will not be able to open it in fid1 if it does not already exist. Additionally, make sure that it is in the same path as your function. – K.G Jun 23 '16 at 17:19
  • I believe so - I used fid1 = fopen('log.bin', 'w'); to create a log.bin. I see the file under the folder menu on the left of my screen. Any idea what I am missing? – amkas90 Jun 23 '16 at 17:23
  • 1
    The only other thing that I can think of is that your fclose may be misplaced/misused. It looks like you're closing the file while still attempting to write to it because you don't actually have a s.stop in your data collection. Try stopping your collection according to your NotifyWhenDataAvailableExceeds – K.G Jun 23 '16 at 17:43
  • Yes. That was the issue it was closing the log before the file was even accessed by the listener. If I add an appropriate pause command I am able to access this information in the lh2 listener. It is logging data to a data array now. Thanks for this. Perhaps you would know why the 'data' array is a 8x122 double, however, when I look at the session it says that it will run for 60 seconds (108) scans at 1.8 scans/second. Why do I have 122 columns being formed. Is this not how I get the actual data? How do I know what the time stamps are for the collected data? – amkas90 Jun 23 '16 at 17:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115437/discussion-between-amkas90-and-k-g). – amkas90 Jun 23 '16 at 17:59