# 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] = str2num(name) // fill wave v0_values
v0_values[ii] = extremum
v0_values[ii] = v0
endfor
End

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?

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.

Andy

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.

`DisplayHelpTopic "The Debugger"` Forum Support Gallery