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
0 个评论
采纳的回答
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')
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!