Help debugging real time plotting issues

I am plotting Raw EMG data in MATLAB. Here is the code I am using for plotting after creating time vector.
% Function to update the real-time plots
function updatePlots(t, data)
if isvalid(fig)
% Create time vectors
tData = (1:length(data)) / samplingRate; % Create a time vector for the raw data
% Plot raw data
addpoints(h1, tData , data);
% Set the X-axis limits based on the time window
xlim(ax1, [t - timeToEdit.Value / 1000, t]);
drawnow limitrate;
end
end
end
Why does the plot have lines connected to the first datapoint. How do I go about debugging this

 Accepted Answer

"Why does the plot have lines connected to the first datapoint[?]"
Because tData(1) is always 1/samplingRate.
I guess you should be using t in your calculation of tData, but I can't say for sure how because I don't know what t represents. I gather it's a scalar number since otherwise the call to xlim would produce an error. You use t as the upper x-limit of your axes, so is it the end time in seconds of the new data? If so then maybe something like this:
tData = t - (numel(data)-1:-1:0) / samplingRate;

1 Comment

Hi Voss, thanks for your answer. The lines from origin have disappared but now there are lines connecting random points. Not sure why. There is a timer running used to plot amplitude vs time of the EMG data received from Arduino. The timer runs from 0 to t, where t is the elapsed time when data x is acquired from arduino. The data read from arduno is alright. It seems to be an issue with plotting.
% Start reading and processing data
while isAcquiringData
data = readline(arduinoObj);
data = str2double(data);
% Append data to buffers
dataBuffer = [dataBuffer, data];
% Update elapsed time
t = toc(startTime);
% Debugging: Display received data
%disp(['Received data: ', num2str(data)]);
% Check if the time window has elapsed
if t >= timeToEdit.Value / 1000
stopButtonPushed();
end
% Update real-time plots
updatePlots(t, dataBuffer);
end
end
end

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023a

Asked:

on 16 Oct 2023

Commented:

on 17 Oct 2023

Community Treasure Hunt

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

Start Hunting!