Clear Filters
Clear Filters

How to convert a 1x1 struct to double matrix (error using pan tompkin)?

2 views (last 30 days)
I'm trying to use Pan Tompkins code to detect QRS from ECG signal, and I'm facing a problem, the code defines ecg as vector, but there's part of the code function that uses this line:
ecg_h = filtfilt(a,b,ecg);
But ecg has to be a double matrix for filtfilt function to work. I tried loading the signal before defining it as ecg because it gets loaded as double matrix, but that didn't work, I kept getting undevined variable error.
% Load data
ecg=load('ECGdata.mat');
fs = 500; % Sampling frequency
t = (0:length(ecg)-1) /fs;
gr=1;
QRS=pan_tompkin(ecg,fs,gr);
  3 Comments
Student
Student on 1 Jun 2024
Originally, I didn't put the semicolon, I only added it because MATLAB error system recommended it. I did remove it and there's no difference. As for the files you mentioned I've attached them now.
For the code, I changed it to this:
% Load data
matstruct=load('ECGdata.mat')
ecg = matstruct.x;
fs = 500; % Sampling frequency
t = (0:length(ecg)-1) /fs;
gr=1;
QRS=pan_tompkin(ecg,fs,gr);
heartRate = calculateHeartRate(QRS, fs);
But it made no difference from what I tried before, the code works, but the signal I have is supposed t0 be normal, but I keep getting wrong heart rate value, unless the heart rate code I'm using is wrong.
Student
Student on 2 Jun 2024
someone mentioned I only replied to half of what you asked for, and attached the displayed results, I apologize for the trouble.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 2 Jun 2024
Edited: Stephen23 on 2 Jun 2024
"Originally, I didn't put the semicolon, I only added it because MATLAB error system recommended it. I did remove it and there's no difference."
Of course it will not change any results from your code: the semicolon simply suppresses displaying those code results, it will not change what the results are. Image Analyst asked you to display the results and to "tell us what you see in the command window"... which you did not do. There is not much point in following only half of their advice.
Unless you are debugging you should use the semicolon.
"I kept getting undevined variable error."
After fixing your incorrectly-named Mfile (wrong name, spaces, wrong extension) everything works as expected:
matstruct = load('ECGdata.mat');
ecg = matstruct.x;
fs = 500; % Sampling frequency
t = (0:numel(ecg)-1) / fs;
gr = 1;
QRS = pan_tompkin(ecg,fs,gr)
QRS = 1x134
0.9810 0.9115 0.9846 0.9565 0.8669 0.9559 0.9880 0.9077 0.9764 0.9657 0.9303 0.9821 0.9392 0.9139 0.9839 0.9445 0.9440 0.9893 0.9376 0.9282 0.9880 0.9411 0.9371 0.9853 0.9711 0.9346 0.9930 0.9887 0.9160 0.9792
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
heartRate = calculateHeartRate(QRS, fs)
heartRate = -4.7994e+07
  1 Comment
Student
Student on 2 Jun 2024
Edited: Student on 2 Jun 2024
I'm sorry to have troubled you to show the results in my stead, I guess I didn't completly understand what they meant, and thought I answered well.
I did read it supresses the display results, but to be honest I only know the very basics of MATLAB, so I didn't grasp the purpose, and still don't unfortunately even with yuour helpful explanation (edit: actually no I get it now, thank you), I'd like to learn using it, but I don't have the time before submiting my assigment in two days.
Thank you for the reply.

Sign in to comment.

More Answers (0)

Categories

Find more on ECG / EKG in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!