Plotting a text file with time on x axis?
조회 수: 20 (최근 30일)
이전 댓글 표시
I am plotting an ECG graoh using values from a text file and want to change the x axis to be time instead of a sample number. defining a time and trying to plot hasnt worked so far. I was wanting it to start at time zero and each data point be pointed at a particular sample time?
My code so far:
%Read the ECG file in
file=readtable("C:\Users\NAME\Documents\ECG_40.txt");
plot(file.Var1)
title('ECG Plot')
xlabel('Time/ms')
ylabel('Voltage/mV')
grid on
%Show R Peaks
[pks, locs] = findpeaks(file.Var1, 'MinPeakHeight', 400);
plot(file.Var1);
hold on;
plot(locs, pks, 'x'); % add 'x' markers at peak positions
grid on
댓글 수: 0
답변 (1개)
dpb
2025년 11월 22일 15:51
편집: dpb
2025년 11월 22일 16:03
You've got to get the sampling rate from somewhere -- if it isn't in the data file, then you'll have to know it from some other source.
For exposition here, let's assume your data were taken at 500 Hz...
%Read the ECG file in
tECG=readtable("C:\Users\NAME\Documents\ECG_40.txt"); % let's use t for table, and what the data is as variable name
tECG.Properties.VariableNames={'ECG'}; % give the variable a meaningful name, too...
Fs=500; % Sample rate, Hz -- use what is correct for your data
dt=1/Fs; % time sampling interval --> 1/Rate, sec
N=height(tECG); % how many samples there are...
T=(N-1)*dt*1000; % end trace time, msec
tECG=addvars(tECG,linspace(0,T,N).', ...
'NewVariableNames','Time','Before','ECG'); % add the time variable to table
plot(tECG.Time,tECG.ECG) % now plot the trace vs time
title('ECG Plot')
xlabel('Time/ms')
ylabel('Voltage/mV')
grid on
%Show R Peaks
[pks, locs] = findpeaks(tECG.ECG, Fs,'MinPeakHeight', 400); % give it the sampling rate so knows where are
hold on;
plot(locs, pks, 'x'); % add 'x' markers at peak positions
should be close barring typos and the inadvertent slipup...remember to use the correct sampling rate for your actual data...
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Scatter Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!