Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
peak position

Subject: peak position

From: manesh g

Date: 16 Jan, 2009 05:05:04

Message: 1 of 7

Hi all..
I have 100 plots in a figure window. Each curve has a peak within x range say between 10-100. The peak positions are not at the same x-values but all of them lie within this range. I wish to write a script which can find the peak x values of each plot (100 of them) in the graph window provided the X range is defined.
How to write such a script in matlab? The use of ginput to find the maximum x value is tedius in my case.
Any help shall be highly appreciated..
thank you,
Mane

Subject: peak position

From: Matt Fig

Date: 16 Jan, 2009 05:18:02

Message: 2 of 7

Are you saying that you don't have the data, you only have the plots? You can get the data by looking in the 'xdata' and 'ydata' properties of the line objects (children of the axes). Then you can use the max function to find the peaks. For example:


x = 0:.1:10;
plot(x,x.^2,x,x.^2.1,x,x.^2.2) % To catch up to where you are.
ch = get(gca,'chi')
get(ch(:),'ydata')

Now the y data is stored in a cell array.




`PBBFQ`PHBNPUIgB:ZIPnWDFMU&`BPV!OUQVMOFCLzB`CPJ`I``N.gDJF`B

Subject: peak position

From: Sadik

Date: 16 Jan, 2009 05:19:01

Message: 3 of 7

"manesh g" <manesh_g2001@yahoo.com> wrote in message <gkp4i0$2au$1@fred.mathworks.com>...
> Hi all..
> I have 100 plots in a figure window. Each curve has a peak within x range say between 10-100. The peak positions are not at the same x-values but all of them lie within this range. I wish to write a script which can find the peak x values of each plot (100 of them) in the graph window provided the X range is defined.
> How to write such a script in matlab? The use of ginput to find the maximum x value is tedius in my case.
> Any help shall be highly appreciated..
> thank you,
> Mane

Hello Mane,

I will try to give you the outline and hopefully you will fill in the blanks using the help menu.

There will be 100 line objects corresponding to the 100 lines. You can find the handles to these line using the findobj function with the arguments 'Type','Line'. However, you should be careful. If you have a legend, this function should return around 300 line object handles. However, it is always easy to select one of the handles returned by findobj and change its linestyle or color to identify which one it is by for example the code set(selectedHandle,'LineWidth',5).

Once you find these handles, for each line object, there should be an XData property, giving you the range of x over which that specific line is plotted, and a YData property, giving you the values, over which you would like to find the maximum. Then the rest should be easy. Something like this [in a for loop] should work:

currentXData = get(currentLineHandle,'XData');
[maxValue,maxIndex] = max(get(currentLineHandle,'YData'));
currentMaxOccursAt = currentXData(maxIndex);

Hope this helps. Sorry for the very summarized explanation. You may need to spend some time.

Thanks.

Subject: peak position

From: manesh g

Date: 16 Jan, 2009 05:44:02

Message: 4 of 7

Dear Matt Fig,
thanks for the clue. I can use the data itself, but I want the x-value, and I do not understand ur script, "chi" for example. When I tried ur script it does not work out. I am a beginner to Matlab, can you explain me the steps?
thanks,
manesh


"Matt Fig" <spamanon@yahoo.com> wrote in message <gkp5aa$ig7$1@fred.mathworks.com>...
> Are you saying that you don't have the data, you only have the plots? You can get the data by looking in the 'xdata' and 'ydata' properties of the line objects (children of the axes). Then you can use the max function to find the peaks. For example:
>
>
> x = 0:.1:10;
> plot(x,x.^2,x,x.^2.1,x,x.^2.2) % To catch up to where you are.
> ch = get(gca,'chi')
> get(ch(:),'ydata')
>
> Now the y data is stored in a cell array.
>
>
>
>
> `PBBFQ`PHBNPUIgB:ZIPnWDFMU&`BPV!OUQVMOFCLzB`CPJ`I``N.gDJF`B

Subject: peak position

From: Matt Fig

Date: 16 Jan, 2009 06:16:03

Message: 5 of 7

Sorry, I may have been unclear. 'chi' is a shortcut for 'children'. Clear your workspace (if you can without data loss!) then paste this in the command window. It finds the x-values of the peaks of the two lines plotted.

x = 0:.001:3;
plot(x,cos(x-1),x,sin(x)) % Peaks at pi.2 and 1.
ch = get(gca,'children'); % Get the children of the axes(lines).
yd = get(ch(:),'ydata'); % The y-data.
xd = get(ch(:),'xdata');% The x-data.

xmx = zeros(1,length(ch)); % Preallocation.

for ii = 1:length(yd)
    [idx,idx] = max(yd{ii}); % The index of the max val of y.
    xmx(ii) = xd{ii}(idx); % The x value at that index.
end

xmx % display the x-values of the peaks





5CBw48<8HL<YRl4;GRABB6@H:;Y64GR4RB?GR`BC4R4?IR5 A>4@8RB8r;,

Subject: peak position

From: manesh g

Date: 17 Jan, 2009 04:26:02

Message: 6 of 7

Hi Matt Fig,
thanks much. It works only for a special case; the script finds the maximum x values of the plots (I mean peak positions), but in my case I have many peaks in a single data. So I want to define a x-range and wish to find the peak x-values of that range. How to modify your script to do that?
thanks,
Mane


"Matt Fig" <spamanon@yahoo.com> wrote in message <gkp8n2$7jk$1@fred.mathworks.com>...
> Sorry, I may have been unclear. 'chi' is a shortcut for 'children'. Clear your workspace (if you can without data loss!) then paste this in the command window. It finds the x-values of the peaks of the two lines plotted.
>
> x = 0:.001:3;
> plot(x,cos(x-1),x,sin(x)) % Peaks at pi.2 and 1.
> ch = get(gca,'children'); % Get the children of the axes(lines).
> yd = get(ch(:),'ydata'); % The y-data.
> xd = get(ch(:),'xdata');% The x-data.
>
> xmx = zeros(1,length(ch)); % Preallocation.
>
> for ii = 1:length(yd)
> [idx,idx] = max(yd{ii}); % The index of the max val of y.
> xmx(ii) = xd{ii}(idx); % The x value at that index.
> end
>
> xmx % display the x-values of the peaks
>
>
>
>
>
> 5CBw48<8HL<YRl4;GRABB6@H:;Y64GR4RB?GR`BC4R4?IR5 A>4@8RB8r;,

Subject: peak position

From: Image Analyst

Date: 17 Jan, 2009 05:07:02

Message: 7 of 7

Mane
Unlike the others, I'm assuming you have the data, because after all, you plotted them. There is a findpeaks() function in the signal processing toolkit. Also take a look at this peak finding code for MATLAB:
http://billauer.co.il/peakdet.html
This will find the peaks and valleys of each curve, and allow you some determination as to what exactly constitutes a "peak," so that you can find just the major peaks and avoid little tiny noisy peaks on the major peaks if that's what you want to do.
-ImageAnalyst

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us