John Alberto
John Alberto

Reputation: 437

Getting an x values from a plot in MATLAB

I'm in a sort of a predicament.

So I have a set of y data in MATLAB, and the plot comes out as a bell shaped curve. The peak goes from 0 to .25, and what I need is the duration of this peak with a threshold of .05. So basically what this means is that since this is a bell shaped curve, there are 2 instances where y = .05. I need to know what the x values are at these points and get the difference between the two, thus giving me the duration. The problem I'm having is I don't know how to get the x values. I don't have the x values, and .05 isn't an actual data point in the y vector. Is there any way I can do this, or is this impossible?

I would really appreciate any help anybody could offer, I've been stuck trying to do this for the past 6 hours.

Upvotes: 0

Views: 511

Answers (2)

Drake
Drake

Reputation: 857

If you don't have the abscissae and you are creating the plot by only providing y as input to the plot function, i.e., plot(y), then the y values are plotted against the vector 1:length(y).

To get the number of values above threshold = 0.05 you can type

above_thresh_count = length(y(y>=threshold));

If the implied time step is Ts, then

(above_thresh_count - 1)*Ts

will give you an under-approximation (in general; unless the data set is artificial) of the duration you are seeking. If the resolution of your data is high, it is likely that it will be a good approximation. Otherwise, you could look at the interpolation commands, if you want the whole process automated and repeatedly executed. If you only care about this specific vector y, you could manually adjust the calculation since the plot command already did linear interpolation by default when invoked.

Hope that helps.

Upvotes: 0

Luis Mendo
Luis Mendo

Reputation: 112659

>> plot(1:5, 2:6) %// example plot
>> h = get(gca,'children'); %// get a handle to the plot
>> x = get(h,'Xdata') %// get x values
x =
     1     2     3     4     5
>> y = get(h,'Ydata') %// get y values
y =
     2     3     4     5     6

Upvotes: 1

Related Questions