Why is NaN returned when I have all necessary input data?

3 views (last 30 days)
I have a large data set contained in a text file that I am attempting to analyze the difference in frequency of the signal. In the following approach I am simply computing the distance between each peak. the code I have written does not throw any errors, but it doesn'ty actual return any values, either. I have been messing with it for a while and it doesn't make any sense why it would not run correctly. Does anyone see my mistake?
data1 = 'time0.txt';
opts = detectImportOptions(data1);
opts = setvaropts(opts,opts.VariableNames(1),'InputFormat','MM/dd/uuu HH:mm:ss.SSSSSS');
data = readtable(data1,opts);
data.Properties.VariableNames = {'Time','Response'};
data.Time = data.Time - data.Time(1);
data.Time.Format = 'mm:ss.SSSSSS';
% t0 = data(:,1);
% t0 = table2array(t0);
% t0 = seconds(t0);
y0 = data(:,2);
y0 = table2array(y0);
[pks,locs]=findpeaks(y0,"MinPeakProminence",2)
pks = 0×1 empty double column vector locs = 0×1 empty double column vector
Average0 = mean(diff(locs))
Average0 = NaN

Answers (2)

Stephen23
Stephen23 on 20 Apr 2023
Edited: Stephen23 on 20 Apr 2023
"Does anyone see my mistake?"
Your data has a range of approx 0.166: how many peaks do you expect to get with minimum peak prominence >2 (as you specified), when the data actually has a range less than a tenth of that?
fnm = 'time0.txt';
opt = detectImportOptions(fnm, 'Delimiter','\t');
opt = setvaropts(opt, 1, 'InputFormat','M/d/y H:m:s.SSSSSS');
tbl = readtable(fnm,opt);
tbl.Properties.VariableNames = {'Time','Response'}
tbl = 21000×2 table
Time Response _________________________ ________ 4/12/2023 15:42:59.575769 0.17703 4/12/2023 15:42:59.575819 0.13004 4/12/2023 15:42:59.575869 0.086921 4/12/2023 15:42:59.575919 0.047983 4/12/2023 15:42:59.575969 0.045087 4/12/2023 15:42:59.576018 0.075336 4/12/2023 15:42:59.576069 0.12747 4/12/2023 15:42:59.576119 0.16737 4/12/2023 15:42:59.576169 0.18539 4/12/2023 15:42:59.576219 0.16866 4/12/2023 15:42:59.576269 0.13745 4/12/2023 15:42:59.576319 0.091105 4/12/2023 15:42:59.576369 0.051523 4/12/2023 15:42:59.576419 0.041225 4/12/2023 15:42:59.576469 0.079198 4/12/2023 15:42:59.576519 0.11653
tbl.Dur = tbl.Time - tbl.Time(1); % what is this used for?
tbl.Dur.Format = 'hh:mm:ss.SSSSSSS'
tbl = 21000×3 table
Time Response Dur _________________________ ________ ________________ 4/12/2023 15:42:59.575769 0.17703 00:00:00.0000000 4/12/2023 15:42:59.575819 0.13004 00:00:00.0000499 4/12/2023 15:42:59.575869 0.086921 00:00:00.0000999 4/12/2023 15:42:59.575919 0.047983 00:00:00.0001499 4/12/2023 15:42:59.575969 0.045087 00:00:00.0001999 4/12/2023 15:42:59.576018 0.075336 00:00:00.0002499 4/12/2023 15:42:59.576069 0.12747 00:00:00.0003000 4/12/2023 15:42:59.576119 0.16737 00:00:00.0003500 4/12/2023 15:42:59.576169 0.18539 00:00:00.0004000 4/12/2023 15:42:59.576219 0.16866 00:00:00.0004500 4/12/2023 15:42:59.576269 0.13745 00:00:00.0005000 4/12/2023 15:42:59.576319 0.091105 00:00:00.0005500 4/12/2023 15:42:59.576369 0.051523 00:00:00.0006000 4/12/2023 15:42:59.576419 0.041225 00:00:00.0006499 4/12/2023 15:42:59.576469 0.079198 00:00:00.0007000 4/12/2023 15:42:59.576519 0.11653 00:00:00.0007500
[pks,locs] = findpeaks(tbl.Response)
pks = 2311×1
0.1854 0.1893 0.1802 0.1841 0.1880 0.1835 0.1867 0.1825 0.1835 0.1770
locs = 2311×1
9 18 28 36 46 54 63 73 81 90
min(tbl.Response)
ans = 0.0322
max(tbl.Response)
ans = 0.1986

Cris LaPierre
Cris LaPierre on 20 Apr 2023
Edited: Cris LaPierre on 20 Apr 2023
Because no peaks were identified in your signal. It would appear the setting in findpeaks are not appropriate for your signal.
data1 = 'time0.txt';
opts = detectImportOptions(data1);
opts = setvaropts(opts,opts.VariableNames(1),'InputFormat','MM/dd/yyyy HH:mm:ss.SSSSSS');
data = readtable(data1,opts);
data.Properties.VariableNames = {'Time','Response'};
data.Time = data.Time - data.Time(1);
data.Time.Format = 'mm:ss.SSSSSS';
plot(data,"Time","Response")
So while there is nothing wrong with your code, you likely need to adjust your settings. I would suggest doing this interactively in a live script using the Find Local Extrema live task. You will be able to immediately see the results and, once you have find the correct settings, obtain the corresponding code if desired.

Community Treasure Hunt

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

Start Hunting!