Passing indexed sub-wave directly into argument of a function

Hello all,

Igor beginner here, excited to be involved with the community. I deal with many 2D and 3D waves, often times having to select a 1D line profile out of a 2D wave. Here is the idea of what I would do to select a 1D wave from a 2D wave and pass it into a function in the same line of code:

Print x2pnt(input2DWave[][10], 1.2)

This would work on a language like python where the indexed sub-array would be evaluated as an independent array within the function argument. I suspect Igor doesn't allow as function arguments are restricted to variable references (is this correct?). I have also tried with input2DWave[p][10], but didn't work either. The workaround is to declare/assign the 1D sub-wave into a temporary wave, pass that into the function, then erase the temporary wave:

Make/N=(DimSize(input2DWave, 0)) temp
temp = input2DWave[p][10]
Print x2pnt(temp, 1.2)
KillWaves temp

This gets the job done, but gets quite messy when I'm selecting sub-waves several times in a function, also you have to make a global wave, which I do not like the idea of (but seems like a common theme in Igor). I couldn't find anything useful in the programmer manual, would there be a simpler way to achieve what I intended?

Thank you in advance!

Your idea of a sub-wave as a wave is nice one, and would be difficult to implement given Igor's architecture. Your work-around of creating a temporary wave is the right way to go, but you can make it a free wave. A free wave cleans up after itself. Also, you can use the Duplicate operation to create the subsection of the wave that you want:

Function test(WAVE w)
    Make/FREE/D/N=(DimSize(w, 1)) temp = w[10][p]
    print temp
end

You could also use Duplicate:

Function test(WAVE w)
    Duplicate/R=[10][]/FREE w,temp
    print temp
end

That has the drawback that it makes a wave that is sort of 2D: it has 1 row and as many columns as the input w. But it's simpler... If you like the simplicity of Duplicate, you can make it 1D using Redimension:

Function test(WAVE w)
    Duplicate/R=[10][]/FREE w,temp
    Redimension/N=(numpnts(temp)) temp
    print temp
end

More about free waves: DisplayHelpTopic "Free Waves"

FYI, MatrixOP is very powerful to extract subsets of 2D or 3D waves, e.g. 

MatrixOP/FREE col10 = col(w2D,10)
MatrixOP/FREE row10 = row(w2d,10)^t

a downside is that it's not aware of wave scaling.

Yes, that's so. It also has the "degenerate 2D wave" problem that sometimes requires a Redimension if you really need a wave with rows and no columns. Some of our old operations still treat waves with dimensions (N,1) as being different from dimensions (N)

From all said, a function below should work as long as the waves in question are in the current data folder.

Function x2pntN(wave ww, variable Ncol, variable xpnt)

    variable pnt
    duplicate/R=[][(Ncol)]/FREE ww, twave
    return (x2pnt(twave, xpnt))
end

caveat -- I have not tested this (see John's follow up comment).

Does that work if you extract a row instead of a column? I wouldn't be surprised if it did: x2pnt() is ancient and probably doesn't look at the dimensions, only the number of points and the X scaling. Snow in Portland is keeping me at home where I don't have access to source code...