finding a specific peak point in a table
2 views (last 30 days)
Show older comments
Hello,
attached is a table of data where we are intrested in locating the peak point according to a certain criteria,
- it should be located within a range of 200-700 for the variable GDALT
- It is the first peak that is found in that interval
to do that I am applying the folling lines
profileTable.INDEX = (1:height(profileTable))';
subTable = profileTable(((profileTable.GDALT>=200) & (profileTable.GDALT<=700)), :);
[pks,locs] = findpeaks(subTable.NE8);
result = subTable(locs,:);
x = profileTable.NE8 ;
y = profileTable.GDALT ;
peak_y = profileTable.GDALT(result.INDEX(1)) ;
peak_x = profileTable.NE8(result.INDEX(1)) ;
idx = y <= peak_y ;
But I am getting the following error
Index exceeds the number of array elements. Index must not exceed 0.
Error in tabular/dotParenReference (line 114)
b = b(rowIndices);
Error in PeakConstruction (line 94)
peak_y = profileTable.GDALT(result.INDEX(1)) ;
I am attaching the table "profileTable" where I am getting the error, and another table 'profileTable2' where the code is running fine.
1 Comment
KSSV
on 26 Jul 2022
result = subTable(locs,:);
The above table is empty....check your code properly.
Accepted Answer
Image Analyst
on 26 Jul 2022
There are some nan values in the one that doesn't work and you have to basically ignore them. One way is by interpolating those nan values. Try this:
% Demo by Image Analyst
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%====================================================================================
% Read in data.
profileTable = readtable('profileTable.txt')
profileTable.INDEX = (1:height(profileTable))';
rowsToExtract = ((profileTable.GDALT>=200) & (profileTable.GDALT<=700));
subTable = profileTable(rowsToExtract, :);
fprintf('Extracted %d rows from profileTable into subTable.\n', height(subTable));
%====================================================================================
% Get data that we're interested in.
x = subTable.INDEX;
y = subTable.NE8;
% Plot x and y data as blue markers with lines between them.
% Nan locations will not have a line to them and will have no marker there.
plot(x, y, 'b.-', 'LineWidth', 2, 'MarkerSize', 15);
%====================================================================================
% There are some NANs in y that prevent it from finding peaks.
% Replace those by interpolation
nanLocations = isnan(y); % Logical indexes at first so we can extract good values.
% Get the good (x,y) locations for use in the interpolation.
xGood = x(~nanLocations);
yGood = y(~nanLocations);
nanLocations = find(isnan(y)); % Get linear indexes this time.
for k = 1 : length(nanLocations)
% Get the index.
thisIndex = nanLocations(k);
% Get the x value of this nan location.
xNan = x(thisIndex);
y(thisIndex) = interp1(xGood, yGood, xNan, 'linear');
fprintf('Interpolated value for x=%d is %f.\n',xNan, y(thisIndex))
end
% Show interpolated values as a red spot.
hold on;
plot(x(nanLocations), y(nanLocations), 'r.', 'LineWidth', 2, 'MarkerSize', 20);
grid on;
title('subTable.NE8 Interpolated NAN values are in red.')
%====================================================================================
% Now find Peak Values.
[peakYValues, indexesOfPeaks] = findpeaks(y);
if isempty(peakYValues)
warningMessage = sprintf('WARNING: there are no peaks in the signal');
uiwait(warndlg(warningMessage));
return;
end
% Get the x values at the peak index locations.
xPeaks = x(indexesOfPeaks);
% Plot triangle over the peaks.
plot(xPeaks, peakYValues, 'rv', 'LineWidth', 2, 'MarkerSize', 14);
% Print out all peak x and y values.
for k = 1 : length(xPeaks)
fprintf('Peak value #%d is at x = %d is y = %f.\n', k, xPeaks(k), peakYValues(k))
end
Extracted 33 rows from profileTable into subTable.
Interpolated value for x=22 is 1898000000000.000000.
Interpolated value for x=25 is 1335000000000.000000.
Peak value #1 is at x = 21 is y = 2365000000000.000000.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!