Why is NaN returned when I have all necessary input data?
3 views (last 30 days)
Show older comments
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)
Average0 = mean(diff(locs))
0 Comments
Answers (2)
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.Dur = tbl.Time - tbl.Time(1); % what is this used for?
tbl.Dur.Format = 'hh:mm:ss.SSSSSSS'
[pks,locs] = findpeaks(tbl.Response)
min(tbl.Response)
max(tbl.Response)
0 Comments
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!