string as a wave reference...
igorisfun
I have 2 waves loaded from a csv data file. 1 wave, strwv, contains a list of strings that repeat but are not in any particular order, e.g.
yes,no,hi,bye,no,yes
and the second wave, varwv, contains values that go along with those strings. (arbitrary e.g. 1,2,4,8,199,0.2)
I would like to make a new set of waves that are named based on the points in strwv and filled with the corresponding points in varwv. So the wave "yes" from the arbitrary example above would contain 2 points: 1 and 0.2.
This what I have so far with minor exception catching:
Function ParseLoadedData()
string wvlist = WaveList("*",";","")
if (strsearch(wvlist,"variable",0)>0) // make sure variable wave loaded
wave/T variableW
wave value
variable totalpnts= numpnts(variableW), i, tempnum
string tempstr
for(i=0; i<totalpnts; i+=1)
tempstr = variableW[i]
if(waveexists($tempstr)==0) //check to see if the variable has a wave made already, if not then make it
Make/D/N=1 $tempstr
$tempstr[0] = value[i] //and assign that value to position 0
endif
if(waveexists($tempstr)==0) //if variable is already a wave...
tempnum = numpnts($tempstr)
insertpoints tempnum,1,$tempstr //want to insert new points at the end not beginning
$tempstr[tempnum+1] = value[i]
endif
endfor
endif
End
string wvlist = WaveList("*",";","")
if (strsearch(wvlist,"variable",0)>0) // make sure variable wave loaded
wave/T variableW
wave value
variable totalpnts= numpnts(variableW), i, tempnum
string tempstr
for(i=0; i<totalpnts; i+=1)
tempstr = variableW[i]
if(waveexists($tempstr)==0) //check to see if the variable has a wave made already, if not then make it
Make/D/N=1 $tempstr
$tempstr[0] = value[i] //and assign that value to position 0
endif
if(waveexists($tempstr)==0) //if variable is already a wave...
tempnum = numpnts($tempstr)
insertpoints tempnum,1,$tempstr //want to insert new points at the end not beginning
$tempstr[tempnum+1] = value[i]
endif
endfor
endif
End
The problem here is that $tempstr[i] throws a "can not use $ in this way" error. Do I need to use the
/wave
flag on the Make statement? If so, how do I keep track of that between the 2 if statements?Thanks for any help.
string wvlist = WaveList("*",";","")
if (strsearch(wvlist,"variable",0)>0) // make sure variable wave loaded
wave/T variableW
wave value
variable totalpnts= numpnts(variableW), i, tempnum
string tempstr
for(i=0; i<totalpnts; i+=1)
tempstr = variableW[i]
WAVE/Z tempwave = $tempstr
if(!waveexists(tempwave)) //check to see if the variable has a wave made already, if not then make it
Make/D/N=1 $tempstr
WAVE tempwave=$tempstr
tempwave = value[i] //and assign that value to position 0
endif
if(waveexists(tempwave)) //if variable is already a wave...
tempnum = numpnts($tempstr)
insertpoints tempnum,1,$tempstr //want to insert new points at the end not beginning
tempwave[tempnum+1] = value[i]
endif
endfor
endif
End
I have compiled it, but I haven't tried to run it...
If your waves are long, you may have troubles with performance. Both constantly looking up the waves using WAVE statements, and constantly adding one point are costly ways to do this. In a case like this, if performance is a problem, I would probably reduce the need for InsertPoints by make waves with lots of points to start with, and then reduce their length when it's finished. You might also be able to keep store a wave reference for each wave, allowing you to avoid the wave look-up. But that is complex and would only be potentially important with many, many waves.
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
October 12, 2016 at 03:19 pm - Permalink
* convert text wave to a variable mask wave for each text value
* use the mask to sort and remove unmatched items
* create the new wave for the given string
I think of some thing like this (fix the syntax though because this is off the top of my head) ..
string mstr = "yes" // then "no" then "hi" then ...
string nvwave = mstr + "_values"
duplicate/FREE variableW variableWtmp
duplicate/O variableValues $nvwave
wwmask = cmpstr(variableWtmp,mstr) ? NaN : 1
sort ... // sort variableWtmp and $nvwave based on wwmask
cut ... // cut all values in $nvwave on values that are NaN in wwmask
Voila ... you have your new wave with the data points.
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH
October 12, 2016 at 06:24 pm - Permalink
jjweimer: that's definitely a more elegant solution than mine! That's actually exactly how I checked my solution in excel (filter->sort)...I will have to play around with it!
October 12, 2016 at 06:42 pm - Permalink