Plotting a text file with time on x axis?

조회 수: 20 (최근 30일)
Cheyanne
Cheyanne 2025년 11월 22일 14:41
편집: dpb 2025년 11월 22일 16:03
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

답변 (1개)

dpb
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...

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by