Why NULL Waves?

Hi All,
Can anyone tell me why the following code produces a NULL wave on the first run?
I have identified that the problem is with the wave M_ImageObjPerimeter. The debugger shows it as a NULL wave, but plots values in it, in the wave display panel (I find that weird). If I leave the waves intact (by commenting out my clean-up KillWaves section), on a second run, it is fine. Do I have to declare anything special to do this - other routines that use the same ImageThreshold and ImageAnalyzeParticles calls are fine though?
I'm running this on 6.2beta2.
Thanks in advance,
Jason.

Function ParticlesAnalysis(Image,thresholdval,size)
Wave Image
Variable Thresholdval,size
Variable wlen,i,j,k
Wave W_SpotX, W_SpotY,W_ImageObjArea,W_ImageObjPerimeter,W_circularity,W_rectangularity
ImageThreshold/I/T=(thresholdval)/Q image
ImageAnalyzeParticles/E/W/Q/M=3/A=25 stats, M_ImageThresh
Make/O/N=(numpnts(W_ImageObjArea),7) ImageAnalysisData
ImageAnalysisData[][0]=W_ImageObjArea[p]
ImageAnalysisData[][1]=W_ImageObjPerimeter[p]
ImageAnalysisData[][2]=W_circularity[p]
ImageAnalysisData[][3]=W_rectangularity[p]
ImageAnalysisData[][4]=W_SpotX[p]
ImageAnalysisData[][5]=W_SpotY[p]
    for (i=0;i<numpnts(W_ImageObjArea);i+=1)
        if ((ImageAnalysisData[i][0] > 700 && ImageAnalysisData[i][0] <1500) && (ImageAnalysisData[i][1] > 90 && ImageAnalysisData[i][1] < 155) && (ImageAnalysisData[i][2]>1 && ImageAnalysisData[i][2]<1.3))
            ImageAnalysisData[i][6]=1
        Elseif ((ImageAnalysisData[i][0] > 300 && ImageAnalysisData[i][0] <1500) && (ImageAnalysisData[i][1] > 90 && ImageAnalysisData[i][1] < 300) && (ImageAnalysisData[i][2]>1 && ImageAnalysisData[i][2]<4))
            ImageAnalysisData[i][6]=2
        Else   
            ImageAnalysisData[i][6]=0
        Endif
    endfor
Newimage image
appendtograph ImageAnalysisData[][5] vs ImageAnalysisData[][4]
ModifyGraph mode=3,marker=19,msize=2,rgb=(65565,0,0)
    for  (i=0;i<numpnts(W_ImageObjArea);i+=1)
    if  (ImageAnalysisData[i][6]==1)
        ModifyGraph rgb(ImageAnalysisData[i])=(0,65535,0)
        ModifyGraph msize(ImageAnalysisData[i])=5
    elseif  (ImageAnalysisData[i][6]==2)
        ModifyGraph rgb(ImageAnalysisData[i])=(0,0,65535)
        ModifyGraph msize(ImageAnalysisData[i])=3
    Else
        ModifyGraph rgb(ImageAnalysisData[i])=(65535,0,65535)
        ModifyGraph msize(ImageAnalysisData[i])=2
    endif  
    endfor
KillWaves  M_ImageThresh,W_ImageObjArea,W_SpotX,W_SpotY,W_circularity;DelayUpdate
KillWaves  W_rectangularity,W_ImageObjPerimeter,W_xmin,W_xmax,W_ymin,W_ymax;DelayUpdate
KillWaves  M_Moments,M_RawMoments,W_BoundaryX,W_BoundaryY,W_BoundaryIndex;DelayUpdate
KillWaves  M_Particle
End
<pre><code class="language-igor">
Hello Jason,

You are missing the wave references in your function.

Suppose your function calls an operation (say operation1) that creates as an output a wave called wave_A which you then want to pass to operation2. The syntax should be:

Function myFunction()
operation1 // this creates wave_A
Wave wave_A // create the reference
operation2 wave_A
End

To read more about this, execute:

Displayhelptopic "wave references"

A.G.
WaveMetrics, Inc.
Remember to put a wave reference declaration after the command that creates the wave. At runtime when the wave reference statement executes, the wave must exist.
Ah, Thanks. That's sorted it.
I put the wave declarations on the line before I created the waves when they should have gone right after.
I note in my other procedures, that I have not hit this problem before because I have duplicated and renamed the waves of interest. This automatically created the wave reference.
Jason.