Function for appending a new value to an existing wave

// Handy Function to append a new value to an existing 1-dimensional wave
Function AppendValue(thewave, thevalue)
    Wave thewave
    Variable thevalue
    Redimension /N=(numpnts(thewave)+1) thewave
    thewave [numpnts(thewave)] = thevalue
End
I get an out of bounds error as the index in the last line is off by one.
Better write it as
thewave [numpnts(thewave)-1] = thevalue

Or even as
Function AppendValue(thewave, thevalue)
    Wave thewave
    Variable thevalue

        variable size = DimSize(thewave,0)
    Redimension /N=(size+1,-1,-1,-1) thewave
    thewave [size] = thevalue
End

That also works with multidimensional waves.

Keep in mind that this is might be slow as Redimension might need to allocate memory.
The line         thewave [size] = thevalue will not do what you expect if you feed it a matrix wave. Might be better to write thewave[size][0][0][0] = thevalue

But trying to come up with a reasonable behavior for waves of any dimensionality is probably futile. What is the correct way to add one value to a matrix? Add a row and fill the first value of the row with the new value, and zeroes in the rest of the row? Fill the entire row with the new value? Add a column and then ask the same questions? And the question expands exponentially with increasing dimensionality...

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More