Grabbing the first non NaN value from a list of waves
epiphenom
I have written a small function where I have a several waves that have an arbitrary number of NaN points from none to 8 before actual values. I would like to grab the first non-NaN value from each of these waves and store that separately but am running into some issues. I could be missing something obvious here and would appreciate some help:
function Firstpoint()
string cell_list
variable index
variable first_non_zero_pnt
//wave ctr_inst
//wave plus_zero
cell_list = WaveList("stimnst*",";","") //get wavelist matching current
variable numwaves = itemsinlist(cell_list)
Make/O /N= 40 ctr_inst
//Make/O /N=40 plus_zero
for(index=0; index<numwaves; index+=1)
string yWaveName = StringFromList(index, cell_list)
wave yWave = $yWaveName
//Wave ctr_inst
//wave plus_zero
wave ctr_inst
//ctr_inst[index] = yWave[first_non_zero_pnt]
Findlevel /Edge=1 /P /Q yWave, 0 ; Print V_levelX+1; first_non_zero_pnt = V_levelX+1
ctr_inst[index]= yWave[first_non_zero_pnt]
endfor
end
string cell_list
variable index
variable first_non_zero_pnt
//wave ctr_inst
//wave plus_zero
cell_list = WaveList("stimnst*",";","") //get wavelist matching current
variable numwaves = itemsinlist(cell_list)
Make/O /N= 40 ctr_inst
//Make/O /N=40 plus_zero
for(index=0; index<numwaves; index+=1)
string yWaveName = StringFromList(index, cell_list)
wave yWave = $yWaveName
//Wave ctr_inst
//wave plus_zero
wave ctr_inst
//ctr_inst[index] = yWave[first_non_zero_pnt]
Findlevel /Edge=1 /P /Q yWave, 0 ; Print V_levelX+1; first_non_zero_pnt = V_levelX+1
ctr_inst[index]= yWave[first_non_zero_pnt]
endfor
end
FindLevel
will work because it doesn't look at NaNs the way you want. A couple of alternatives:- loop inside your index loop to go point by point testing for
numtype==2
(slow but but would work)- make a copy of the wave,
WaveTransform zapnans wavecopy
and then difference in numpnts between the two waves can be used to get the first non zero point (would only work if the NaNs are exclusively at the start of the wave.You could get findlevel to work by converting NaNs to zero first.
Someone else can probably give a more efficient solution.
February 8, 2017 at 02:25 pm - Permalink
yWaveNumType=numtype(yWave)!=2
FindValue/V=1 yWaveNumType
first_non_zero_pnt=V_Value
February 8, 2017 at 05:11 pm - Permalink
I throw
Extract
andFindValue
into the ring.Make/O/n=8 data = p
data[0,3] = NaN
data[6] = NaN
Extract/O data, output, numtype(data) == 0
FindValue/V=(0)/T=(inf) data
print V_value
End
For a large number of points in the wave findValue should be faster.
Nit picking: The FindValue solution does not work if there are also infs in the data as:
•findvalue/V=(0)/T=(inf) data
•print V_value
0
February 8, 2017 at 05:19 pm - Permalink
February 9, 2017 at 07:45 am - Permalink