how to find the location of a value

21 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2023-4-14
First, determine the value of the first valley, find the approximate index of the desired value, and then use interp1 to calculate the x-coordinate of that point.
Try this —
F = openfig('tek0000ALL.fig');
Lines = findobj(F, 'Type','line'); % Return Handles To Line Object(s)
xv = Lines.XData; % Line X-Vector
yv = Lines.YData; % Line Y-Vector
[vys,vlocs] = findpeaks(-yv, 'MinPeakProminence',max(yv)/4); % Find 'Valleys' (Negative Peaks) Values & Locations
zxi = find(diff(sign(yv+vys(1)/10))); % Find Approximate Incices Where 'yv' Is Equal to Desired Value
idxrng = zxi(1)-1 : zxi(1)+1; % Index Range For Interpolation For First Intersection
xp = interp1(yv(idxrng), xv(idxrng), -vys(1)/10) % Desired X-Value For This Point
xp = 2.6405e-05
hold on
plot(xp, -vys(1)/10, 'pr') % Plot Point AS Red Star
hold off
Plotting other locations or values of the curve is simply an extension of thie approach, and would be straightforward tio implement.
.

更多回答(1 个)

Mathieu NOE
Mathieu NOE 2023-4-14
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

Community Treasure Hunt

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

Start Hunting!

Translated by