如何使用MATLAB中的数据采集工具箱连续记录数据和绘图数据

时间:2016-06-22 22:18:05

标签: matlab data-acquisition

我正在使用数据采集工具箱收集数据。数据在后台收集。我正在收集来自8个热电偶的数据,并且能够呼叫我的听众不断更新我的情节。但是,我还需要保留收集数据的文件,数组或某种日志。这是我的代码:

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);

函数plotData是:

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

函数logData是:

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

知道如何在收集数据时不断更新数据,同时更新收集的所有数据点的日志文件?

1 个答案:

答案 0 :(得分:1)

除非你告诉它,否则MATLAB不会存储数据。在这种情况下,您应该预先分配变量,结构或文件以将数据写入。

有关创建和写入日志文件的帮助,请参阅此MathWorks链接:Acquire Continuous and Background Data Using NI Devices