Why is HR here giving me NaN as an answer

4 次查看(过去 30 天)
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 个评论
John D'Errico
John D'Errico 2023-6-9
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.

请先登录,再进行评论。

采纳的回答

Nathan Hardenberg
Nathan Hardenberg 2023-6-12
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 个评论
Ibon Galiano Crespo
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 2023-6-13
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:

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by