Why is HR here giving me NaN as an answer

2 views (last 30 days)
Im doing a heart rate monitor and i have to calculate the heart rate in a matlab app but HR is giving me NaN values and I dont know how to fix it because the code seems correct and I have no errors in the code.
This is the code to calculate heart rate the formula is (60 / time between 2 pulses)
[~, locs]=findpeaks(fn,"MinPeakHeight",0.6,"MinPeakProminence",0.5);
med=[];
for n=1:length(locs)-1
med(n)=locs(n+1)-locs(n);
end
HeartRate=(60*app.Fs)/mean(med);
When I call HR later I get an error in the command window that says:
Warning: Error occurred while executing the listener callback for event DataAvailable defined for class daq.ni.Session:
Error using matlab.ui.control.internal.model.AbstractNumericComponent/set.Value (line 185)
'Value' must be a double scalar within the range of 'Limits'.
Error in appWIP/HRsaver (line 87)
app.HeartRateEditField.Value=HeartRate;
Does anyone know why it happens? Thanks for answering
  1 Comment
John D'Errico
John D'Errico on 9 Jun 2023
LoL. If you really have no errors in your code, and it is perfect as written, then there would be no problem.
So yes, you do have an error in your code, in that your code is probably not robust to problematic data. We don't know why that is, since you have not posted your data for anyone to test.

Sign in to comment.

Accepted Answer

Nathan Hardenberg
Nathan Hardenberg on 12 Jun 2023
As @John D'Errico suggests, your data is most likely the problem here. You have to have at least two peaks in you data. Otherwise the result is NaN (see example below with one peak in the data).
fn = [0,1,0,0,0,0,0]; % data with one peak
[~, locs]=findpeaks(fn,"MinPeakHeight",0.6,"MinPeakProminence",0.5);
med=[];
for n=1:length(locs)-1 % loop does not get executed at all!
med(n)=locs(n+1)-locs(n);
end
HeartRate=(60)/mean(med)
HeartRate = NaN
Since "med" stays unchanged, the following gets executed and the result is for rather obvious reasons NaN.
mean([])
ans = NaN
Now it's on you to find out why your data does not work right. Maybe you calculate the mean on every frame of the app and only have the datapoints from 1/30 of a second (just a guess). A Buffer should resolve the problem in this case.
It is also a good idea to check the length of the "locs"-vector before calculating the heartrate. If it is smaller than 2 you should skip the calculation for example.
  2 Comments
Ibon Galiano Crespo
Ibon Galiano Crespo on 13 Jun 2023
Yeah my problem was that the signal being sent had only one peak because one of my filters had the wrong limits, and since that wasn't causing errors because the filter was still valid it took a while until I found the solution. I also added this so it wouldn cause any error messages even if it had no output.
if ~isempty(med)
HeartRate=(60*app.Fs)/mean(med);
else
HeartRate = HeartRate;
end
Thanks for the response
Image Analyst
Image Analyst on 13 Jun 2023
This was difficult to answer since you provided no data. You didn't even provide a screenshot of your data plotted. So what can people do?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!