how to find the location of a value
Show older comments
Hi, I'm new to Matlab and I want to find the location of a value. this value is calculated as 1/10 of the value of the first valley. The problem is that it might not have exact same value in original data, how can I find the location of the value which is the most close one. Thank you.
Accepted Answer
More Answers (1)
Mathieu NOE
on 14 Apr 2023
hello
try this
see the peakseek function in attachment (faster and simpler alternative to findpeaks - you can also use findpeaks if you prefer)

data = extract_data_from_figures('tek0000ALL.fig');
VN = data.names;
data = data.Y;
x = data(:,1);
y = data(:,2);
% find valleys
ys = smoothdata(y,'gaussian',10); % smooth a bit the signal first
[locs, pks] = peakseek(-ys,0.1,abs(min(ys))/2); % or use findpeaks
x_val1 = x(locs(1));
y_val1 = y(locs(1));
%%find x value for 10 % crossing level of this signal
y_threshold = 0.1*y_val1; %
if sign(y_threshold) <0
x_threshold = find_zc(x,-y,-y_threshold);
else
x_threshold = find_zc(x,y,y_threshold);
end
x_threshold = x_threshold(1); % select first one
figure
plot(x,y,x_threshold,y_threshold,'*r');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Zx] = find_zc(x,y,threshold)
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
Zx = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end
function data = extract_data_from_figures(filename)
%%
%
% Input : File name <filename.fig>
% with multiple plots in a single file
% Output : struct data
% : data.names contains names of the display object Yvalues
% : data.Y contains the actual plot values withthe first column
% containing the x-values
%
% Written by Chetanya Puri, 2019
% Last Modified: Nov 6, 2019
%
fig = openfig(filename); % Open figure and assign it to fig object
dataObjs = findobj(fig,'-property','YData'); % Find all graphic objects with YData, in our case line values
xval = dataObjs(1).XData; % Find the X-axis value
Ymat = [xval(:)]; % Create a matrix with first column of x values
for i=1:length(dataObjs)
legend_name{i,1} = dataObjs(i).DisplayName;
yval = dataObjs(i).YData;
Ymat = [Ymat yval(:)]; % Keep appending column vectors
end
close(fig); % close the figure
data.names = ['X';legend_name];
data.Y = Ymat;
end
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!