Odd wave indexing behavior
jmbeebe
Here's some simple code that feels like it should work, but doesn't, and I'm wondering what's going on. I'm trying to populate a 2x6 matrix with two pre-existing 1x6 waves. I've tried this in multiple ways, and it never works the way I expect it to.
The following properly inserts wave0 into the first row of Matrix and wave1 into the second row.
Function ConcatenateWaves()
Variable Number=CountObjects("",1)
wave wave0
wave wave1
Wavestats/Q wave0
Variable Length=v_npnts
Make/O/N=(Number,Length) Matrix
Matrix[0]=wave0[q]
Matrix[1]=wave1[q]
End
Variable Number=CountObjects("",1)
wave wave0
wave wave1
Wavestats/Q wave0
Variable Length=v_npnts
Make/O/N=(Number,Length) Matrix
Matrix[0]=wave0[q]
Matrix[1]=wave1[q]
End
If I try to automate that so I don't have to predefine wave0 and wave1 (there will ultimately be many more of these waves), strange things occur.
Function ConcatenateWaves()
Variable Number=CountObjects("",1)
Wave wave0
Wavestats/Q wave0
Variable Length=v_npnts
Make/O/N=(Number,Length) Matrix
Variable ni=0
String Name
Do
Name=GetIndexedObjName("",1,ni)
Wave w=$Name
Matrix[ni]=w[q]
ni+=1
While(ni<Number)
End
Variable Number=CountObjects("",1)
Wave wave0
Wavestats/Q wave0
Variable Length=v_npnts
Make/O/N=(Number,Length) Matrix
Variable ni=0
String Name
Do
Name=GetIndexedObjName("",1,ni)
Wave w=$Name
Matrix[ni]=w[q]
ni+=1
While(ni<Number)
End
Here, I'm referencing wave0 and wave1 through GetIndexedObjName. When I do it this way, only the first column of Matrix populates - all other columns are zero. WaveRefIndexed behaves similarly. Clearly I'm missing something here, probably something obvious. What is it?
Make/O/N=(2,numpnts(col0)) Matrix
Matrix[0][] = col0[q]
Matrix[1][] = col1[q]
End
You need the empty brackets on the left side; then you can use q on the right.
But really, you want the Concatenate operation.
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
February 14, 2017 at 12:20 pm - Permalink
numpnts
.HJ
February 14, 2017 at 12:25 pm - Permalink
Second, your second function wouldn't even compile for me in Igor 7. You have to be a bit more explicit in your wave assignment; use Matrix[ni][]=w[q]:
Variable Number=CountObjects("",1)
String WavesToConcatenate=WaveList("*",";","") //new
Wave wave0
Wavestats/Q wave0
Variable Length=v_npnts
Make/O/N=(Number,Length) Matrix
Variable ni=0
String Name
Do
Name=StringFromList(ni,WavesToConcatenate) //new
print name
Wave w=$Name
Matrix[ni][]=w[q] //new
ni+=1
While(ni<Number)
End
Last, you might explore MatrixOp or Concatenate as alternative ways to do what you want.
February 14, 2017 at 12:27 pm - Permalink
Thanks John. That fixes it. I tried Concatenate and ended up w/ a 1x12 rather than a 2x6 - probably had syntax problems there as well.
February 14, 2017 at 12:46 pm - Permalink
Concatenate/O/NP=1 {wave0,wave1}, Matrix
forces appending columns, but without /NP flag it should just promote correctly if the waves are the same length.February 14, 2017 at 02:02 pm - Permalink