# How to get the y values between two x values on a plot-MATLAB [closed]

I have a plot (it is a spectra). I want to choose some peaks for fitting. How can I select a region from the Matlab plot? I want to choose some regions and do exponential fit. If I can extract the x and y values in those regions, I can fit the curves.

PS: I already have a program which calls the data and plot the spectra. As this program is written by someone else, I am not able to find where the x and y values are stored. It seems a bit complicated program. I just want to keep the same program and add some more lines or functions so that I can do my job (fitting the curve for some selected peaks)...

• I am unsure how your data is stored, but you could use something like find(spectra>value_min&spectra<value_max) to get the corresponding indexes. Jul 15 '13 at 12:09
• If you cannot modify the code or identify x and y data in it, you can work with the image file itself. If there are not too many peaks, you can measure their pixel coordinates just by hovering over them in Paint, and then rescale to proper units. (Sounds stupid, but why not...) Jul 15 '13 at 17:26
• This is not a question about mathematics.... Jul 16 '13 at 14:04
• Matlab and other computing problems which are mathematical in nature are welcome here, but in general questions about the physical use of the software are discouraged... Jul 16 '13 at 14:22

## 1 Answer

You can get data directly from MATLAB figure using get function. See the following example:

x = -10:0.1:10;
y = (x/5).^2+rand(size(x));
plot(x,y)
ch = get(gca,'child');
xdata = get(ch,'xdata');
ydata = get(ch,'ydata');
ydata(abs(xdata)>2)=[];
xdata(abs(xdata)>2)=[];
hold on
plot(xdata,ydata,'r')