Updating plot question and error
10 次查看(过去 30 天)
显示 更早的评论
Hello,
Im setting up a Daq to run continously and update the plot as well,but after trying to add a second plot to plot captured data when a threshold is passed and using the set(plot) function it says my arrays are of wrong size when I know the data is a 1000X8 array.It was working before just using regular plot(), but I just need the xData to have 1:1000 and whatever the yData variables to be listed. Scratching my head trying to figure this out.
And I know the toggle button does nothing right now,got distracted by this plotting issue.
function mDaq
Fs = 1000; % Sampling frequency (Hz)
T = 1/Fs; % Sampling period
% preTriggerTime_s = 0.4; % Pre-trigger time (seconds)
% postTriggerTime_s = 0.6;
bufferTime_s = 1.0; % Total buffer time (seconds)
% threshold = 0.7; % Trigger threshold value
% preTriggerData = [];
% Calculated parameters
% preTriggerSamples = round(preTriggerTime_s * Fs);
% postTriggerSamples = round(postTriggerTime_s * Fs);
bufferSize = round(bufferTime_s * Fs);
% numSamplesToCapture = bufferSize;
%capturing toggle button bool
isCapturing = false;
% fig = uifigure('Name', 'Start/Stop Recording', 'Position', [100, 100, 400, 200]);
hFig = figure('Name',datestr(now),'units','normalized',...
'MenuBar','none','DockControls','on');
%plot and axes handles
hAxes1 = subplot(1,2,1);
hAxes2 = subplot(1,2,2);
hplot1 = plot(hAxes1,NaN,NaN);
hplot2 = plot(hAxes2,NaN,NaN);
hButton1 = uicontrol('Style','pushbutton','Parent',hFig,...
'String','Stop','FontSize',12,'Position',[50 445 40 40],'Callback',@stopBut,'BackgroundColor',[0.6,0.6,0.6]);
% Create a data capture button and configure a callback function
btn = uibutton(hFig, 'push', 'Text', 'Capture', ...
'Position',[50 335 60 60], ...
'ButtonPushedFcn', @(btn, event) toggleCapture(btn));
devices = daqlist;
disp(devices);
d = daq('mcc');
%%%%%%%%%%%% Configure channels and voltage range%%%%%%%%%%%%%%%
for i = 0:7
d.addinput('Board0', (i), 'Voltage'); % Example for channel 0
end
for j = 1:8
d.Channels(j).Range = [-2,2];
end
assignin('base','d',d);
%%%%%%%%%%%%%%Set rate, and make recording Continous%%%%%%%%%%%%%%%%%%%%%%%
d.Rate = 1000; % Sampling rate in Hz
% Set the ScansAvailableFcn callback
d.ScansAvailableFcn = @(src, event) disp("Scans available!");
% Set the number of scans required to trigger the callback
d.ScansAvailableFcnCount = 1000;
d.ScansAvailableFcn = @processData;
% Start background acquisition
start(d, "continuous");
% Define a custom callback function
function processData(src, event)
data = read(src, "all"); % Read all available data
set(hplot1,'xData',1:bufferSize ,'yData',data.Variables); % Plot the data
drawnow;
end
%stop button
function stopBut(source,event)
stop(d);
end
% Nested function to toggle recording
function toggleCapture(btn)
if ~isCapturing
% Start recording
isCapturing = true;
btn.Text = 'Capture OFF';
disp('Recording started...');
% If button is pressed clear data capture plot
set(hplot2, 'XData', NaN, 'YData', NaN);
else
% Stop recording
isCapturing = false;
btn.Text = 'Capture ON';
disp('Recording stopped.');
end
end
end
0 个评论
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!