How to plot real-time with nidaq, using subplot to separate channels in different graphs

5 次查看(过去 30 天)
I am trying to visualize the input from 16 different analog ch using this script (obtained through the built-in APP "analog input recorder"). I tried using "subplot" instead of "plot" but it does not work. Any idea on how this could be done ?
Thanks in advance!
% Auto-generated by Data Acquisition Toolbox Analog Input Recorder on 12-Jul-2018 10:53:33 clear, clc, close all; daq.reset; %% Create Data Acquisition Session % Create a session for the specified vendor. s = daq.createSession('ni');
%% Set Session Properties % Set properties that are not using default values. s.Rate = 20000; s.IsContinuous = true; n_channels = 16
%% Add Channels to Session % Add channels and set channel properties, if any. addAnalogInputChannel(s,'Dev1','ai0','Voltage'); addAnalogInputChannel(s,'Dev1','ai1','Voltage'); addAnalogInputChannel(s,'Dev1','ai2','Voltage'); addAnalogInputChannel(s,'Dev1','ai3','Voltage'); addAnalogInputChannel(s,'Dev1','ai4','Voltage'); addAnalogInputChannel(s,'Dev1','ai5','Voltage'); addAnalogInputChannel(s,'Dev1','ai6','Voltage'); addAnalogInputChannel(s,'Dev1','ai7','Voltage'); addAnalogInputChannel(s,'Dev2','ai0','Voltage'); addAnalogInputChannel(s,'Dev2','ai1','Voltage'); addAnalogInputChannel(s,'Dev2','ai2','Voltage'); addAnalogInputChannel(s,'Dev2','ai3','Voltage'); addAnalogInputChannel(s,'Dev2','ai4','Voltage'); addAnalogInputChannel(s,'Dev2','ai5','Voltage'); addAnalogInputChannel(s,'Dev2','ai6','Voltage'); addAnalogInputChannel(s,'Dev2','ai7','Voltage');
%% Initialize Session UserData Property % Initialize the custom fields for managing the acquired data across callbacks. s.UserData.Data = []; s.UserData.TimeStamps = []; s.UserData.StartTime = [];
%% Add Listeners % Add listeners to session for available data and error events. lh1 = addlistener(s, 'DataAvailable', @recordData); lh2 = addlistener(s, 'ErrorOccurred', @(~,eventData) disp(getReport(eventData.Error)));
%% Acquire Data % Start the session in the background. startBackground(s) pause(5) % Increase or decrease the pause duration to fit your needs. stop(s)
%% Log Data % Convert the acquired data and timestamps to a timetable in a workspace variable. ai0 = s.UserData.Data(:,1); ai1 = s.UserData.Data(:,2); ai2 = s.UserData.Data(:,3); ai3 = s.UserData.Data(:,4); ai4 = s.UserData.Data(:,5); ai5 = s.UserData.Data(:,6); ai6 = s.UserData.Data(:,7); ai7 = s.UserData.Data(:,8); ai8 = s.UserData.Data(:,9); ai9 = s.UserData.Data(:,10); ai10 = s.UserData.Data(:,11); ai11 = s.UserData.Data(:,12); ai12 = s.UserData.Data(:,13); ai13 = s.UserData.Data(:,14); ai14 = s.UserData.Data(:,15); ai15 = s.UserData.Data(:,16); DAQ_2 = timetable(seconds(s.UserData.TimeStamps),ai0,ai1,ai2,ai3,ai4,ai5,ai6,ai7,ai8,ai9,ai10,ai11,ai12,ai13,ai14,ai15);
%% Plot Data % Plot the acquired data on labeled axes.
plot(DAQ_2.Time, DAQ_2.Variables) xlabel('Time') ylabel('Amplitude (V)') legend(DAQ_2.Properties.VariableNames)
%% Clean Up % Remove event listeners and clear the session and channels, if any. delete(lh1) delete(lh2) clear s lh1 lh2
%% Callback Function % Define the callback function for the 'DataAvailable' event. function recordData(src, eventData) % RECORDDATA(SRC, EVENTDATA) records the acquired data, timestamps and % trigger time. You can also use this function for plotting the % acquired data live.
% SRC - Source object i.e. Session object % EVENTDATA - Event data object i.e. 'DataAvailable' event data object
% Record the data and timestamps to the UserData property of the session. src.UserData.Data = [src.UserData.Data; eventData.Data]; src.UserData.TimeStamps = [src.UserData.TimeStamps; eventData.TimeStamps];
% Record the starttime from the first execution of this callback function. if isempty(src.UserData.StartTime) src.UserData.StartTime = eventData.TriggerTime; end
% Uncomment the following lines to enable live plotting.
plot(eventData.TimeStamps, eventData.Data)
xlabel('Time (s)')
ylabel('Amplitude (V)')
legend('ai0','ai1','ai2','ai3','ai4','ai5','ai6','ai7','ai8','ai9','ai10','ai11','ai12','ai13','ai14','ai15')
end

采纳的回答

Anoop Joy
Anoop Joy 2019-4-10
If you want to use 'subplot' (instead of 'plot') in order to visualize each channel data in a separate plot then use the following lines of code in the 'recordData' function above:
subplot(8,8,1)
plot(eventData.TimeStamps, eventData.Data(:,1))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai0')
subplot(8,8,2)
plot(eventData.TimeStamps, eventData.Data(:,2))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai1')
subplot(8,8,3)
plot(eventData.TimeStamps, eventData.Data(:,3))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai2')
subplot(8,8,4)
plot(eventData.TimeStamps, eventData.Data(:,4))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai3')
subplot(8,8,5)
plot(eventData.TimeStamps, eventData.Data(:,5))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai4')
subplot(8,8,6)
plot(eventData.TimeStamps, eventData.Data(:,6))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai5')
subplot(8,8,7)
plot(eventData.TimeStamps, eventData.Data(:,7))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai6')
subplot(8,8,8)
plot(eventData.TimeStamps, eventData.Data(:,8))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai7')
subplot(8,8,9)
plot(eventData.TimeStamps, eventData.Data(:,9))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai8')
subplot(8,8,10)
plot(eventData.TimeStamps, eventData.Data(:,10))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai9')
subplot(8,8,11)
plot(eventData.TimeStamps, eventData.Data(:,11))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai10')
subplot(8,8,12)
plot(eventData.TimeStamps, eventData.Data(:,12))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai11')
subplot(8,8,13)
plot(eventData.TimeStamps, eventData.Data(:,13))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai12')
subplot(8,8,14)
plot(eventData.TimeStamps, eventData.Data(:,14))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai13')
subplot(8,8,15)
plot(eventData.TimeStamps, eventData.Data(:,15))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai14')
subplot(8,8,16)
plot(eventData.TimeStamps, eventData.Data(:,16))
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai15')
  1 个评论
LO
LO 2019-4-10
编辑:LO 2019-4-10
thanks for your reply. I am now working on the following code as it seems to work without errors (the previous one somehow gets stuck all the time)
this seems to work however I still have the same plotting issue.
n_channels = 16;
EODrec_amp = 'npi DPA-2FS';
duration = 3600; % max duration in seconds of data recorded into one bin file
imax = 168; % max number of files to be written
d = daq.getDevices;
EODrec_DAQ = d.Model; % write DAQ model to log file
s = daq.createSession('ni');
channels = 0:n_channels-1; % data are written to channels AI1 and AI5
% s.addAnalogInputChannel(d.ID,channels, 'Voltage')
d1ch1=addAnalogInputChannel(s,'Dev1','ai0', 'Voltage')
d1ch2=addAnalogInputChannel(s,'Dev1','ai1', 'Voltage')
d1ch3=addAnalogInputChannel(s,'Dev1','ai2', 'Voltage')
d1ch4=addAnalogInputChannel(s,'Dev1','ai3', 'Voltage')
d1ch5=addAnalogInputChannel(s,'Dev1','ai4', 'Voltage')
d1ch6=addAnalogInputChannel(s,'Dev1','ai5', 'Voltage')
d1ch7=addAnalogInputChannel(s,'Dev1','ai6', 'Voltage')
d1ch8=addAnalogInputChannel(s,'Dev1','ai7', 'Voltage')
d2ch1=addAnalogInputChannel(s,'Dev2','ai0', 'Voltage')
d2ch2=addAnalogInputChannel(s,'Dev2','ai1', 'Voltage')
d2ch3=addAnalogInputChannel(s,'Dev2','ai2', 'Voltage')
d2ch4=addAnalogInputChannel(s,'Dev2','ai3', 'Voltage')
d2ch5=addAnalogInputChannel(s,'Dev2','ai4', 'Voltage')
d2ch6=addAnalogInputChannel(s,'Dev2','ai5', 'Voltage')
d2ch7=addAnalogInputChannel(s,'Dev2','ai6', 'Voltage')
d2ch8=addAnalogInputChannel(s,'Dev2','ai7', 'Voltage')
s.Rate = 20000; % sample rate
Fs = s.Rate;
datapath = 'F:';
datapath = uigetdir(datapath,'Pick a directory'); % pick data directory
cd(datapath);
for i = 1:imax
c = clock;
logfilename = ['log_dataID' num2str(dataID) '_' num2str(c(1)) '_' num2str(c(2)) '_' num2str(c(3)) '_' num2str(c(4)) '_' num2str(c(5)) '.txt'];
mat_logfilename = [logfilename(1:end-3) 'mat']; % log variables in mat format
datafilename = [logfilename(5:end-3) 'bin'] % data file name
save(mat_logfilename,'logfilename','datafilename');
fid1 = fopen(datafilename,'w');
lh = s.addlistener('DataAvailable', @(src, event) logData(src, event, fid1));
lh1 = s.addlistener('DataAvailable', @(src,event) plot(event.TimeStamps, event.Data));
%% Acquire Data in the Background
%
% Acquire data continuously in a non-blocking mode.
s.IsContinuous = true;
s.startBackground;
pause(duration);
%% Stop the Continuous Acquisition and Close the Log File
s.stop;
delete(lh);
delete(lh1);
fclose(fid1);
end
%%
displayEndOfDemoMessage(datafilename)
function logData(src, evt, fid)
% Add the time stamp and the data values to data. To write data sequentially,
% transpose the matrix.
% Copyright 2011 The MathWorks, Inc.
data = [evt.TimeStamps, evt.Data]' ;
fwrite(fid,data,'double');
end
I think here I just might have to work on the function logData and add some plotting command after the definition of the variable "data", as here below, but somehow it does not seem to work... (I have removed the second listener pointing to the plot command also)
function logData(src, evt, fid)
% Add the time stamp and the data values to data. To write data sequentially,
% transpose the matrix.
% Copyright 2011 The MathWorks, Inc.
data = [evt.TimeStamps, evt.Data]' ;
subplot(8,8,1)
plot(data(1,:),data(2,:));
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai0')
subplot(8,8,2)
plot(data(1,:),data(3,:));
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai1')
subplot(8,8,3)
plot(data(:,1),data(4,:));
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai2')
subplot(8,8,4)
plot(data(1,:),data(5,:));
xlabel('Time (s)'), ylabel('Amplitude (V)'), legend('ai3')
...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by