Find x-value corresponding to maximum y-value

Hi all,

I created 5 test waves using the command:

Make '1','2','3','4','5'
'1' = exp(-(p-60)^2/50)
'2' = 2*exp(-(p-50)^2/50)
'3' = 3*exp(-(p-70)^2/50)
'4' = 4*exp(-(p-75)^2/50)
'5' = 5*exp(-(p-39)^2/50)

The purpose is to find x-values corresponding to maximum y-value for each wave.

I wrote the following function:

Function find_v0_values()
    Make/O/D/N=(5,3) v0_values // Make wave with 5 rows and 3 columns
    // 1st column is for names of waves (1,2,3,4,5)
    // 2nd column is for maximum y-values
    // 3rd column is for x-values corresponding to maximum y-values
    String list = WaveList("*",";","")
    Variable numItems = ItemsInList(list), ii
    String name
    Variable extremum, v0
    for (ii=0; ii<numItems; ii+=1)
        name = StringFromList(ii,list)
        Wave w = $name
        WaveStats/Q w
        extremum = V_max
        v0 = V_maxloc
        v0_values[ii][0] = str2num(name) // fill wave v0_values
        v0_values[ii][1] = extremum
        v0_values[ii][2] = v0

But this function doesn't work properly. Function Execution Error arises. Please find attached the error message and created wave v0_values. The last 5th row in this wave is wrong. How can I solve this problem?

Error v0_values wave

I wrote:

for (ii=0; ii<numItems-1; ii+=1)

instead of

for (ii=0; ii<numItems; ii+=1)

and it worked. But I don't understand, why I should write ii < numItems - 1 instead of ii < numItems ?

Another way to find the x location of a y maximum is to call wavestats.  It produces: V_maxloc    X location of maximum data value.


In reply to by hegedus

Thanks for reply! Yes, in my function, I use V_maxloc. But the problem is why should I write ii < numItems - 1 instead of ii < numItems ? I thought that if I write:

for (ii = 0; ii < numItems; ii += 1)

and given that numItems = 5,

it will result in ii = 0, 1, 2, 3, 4. But it appears that Igor Pro counts in another way: ii = 0, 1, 2, 3, 4, 5, thus including the point 5. How can it be?

One thing to note is that the first thing in the function is to create a new wave so if you have 5 data waves originally you will now have 6 total waves. The wavelist function could be tailored to avoid including that wave.  Another strategy is to have separate data folders for input data and analysis waves.


In reply to by Physicist92

This is a good place to put in a plug for the debugger.  Using this, you might have discovered that ItemsInList was returning a different value from what you were expecting.


For more info, execute the following from the command line:

DisplayHelpTopic "The Debugger"




Igor Pro 8

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More