Calling waves from data browser in loop
geologic
This is a shortened version of a longer post.
Is there a way, if I have wave_1, wave_2, ..., wave_10 in the data broswer to loop in such a way that each iteration of the loop calls a different wave from the data browser?
Thanks!
If your wave names are really systematic like that, it is usual to use a loop to compute the names, without needing the Data Browser to do it. Something like this:
do
String wname = "wave_"+num2str(index) // compute the next name
Wave/Z w = $wname // /Z avoids error if the wave doesn't exist
if (!WaveExists(w))
break // if the wave doesn't exists, we don't have any more waves, so get out of the loop
endif
... now do something with w ...
index += 1 // increment the index so we go on to the next wave in the sequence
while(1) // infinite loop- better have a break in the loop somewhere!
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
December 1, 2014 at 05:09 pm - Permalink
String name
Variable i=0
do
name=GetBrowserSelection(i)
if(strlen(name)<=0)
break
endif
// check if name corresponds to a wave
// do something with it.
i+=1
while(1)
End
December 1, 2014 at 05:16 pm - Permalink
WaveList
function to get a list of all waves in the current data folder that fit criteria that you can specify.Then, extract items from the list one at a time using
StringFromList
in your loop. The "$" operator is needed to convert a list item to a wave reference.December 1, 2014 at 05:18 pm - Permalink
I like the generality of wavelist, so I'm using that for now. Plus, then I don't have to highlight waves in the data browser.
December 9, 2014 at 12:34 pm - Permalink
string sample
string templist
variable i
//this next part is to demonstrate that i can create all of the wave names that i need
for(i=0;i<=2;i+=1)
print stringfromlist(i,templist)
endfor
//here i believe i am creating a local string name for manipulation: s1
string s1 = sample+"_"+stringfromlist(0,templist)
//here i believe i am creating a local wave name for manipulation: f
wave f = $s1
//however, print f results in NULL wave
print f
end
December 9, 2014 at 02:49 pm - Permalink
string sample, templist
variable np
// local variables
string s1
// create new wave name
s1 = sample+"_"+stringfromlist(np,templist)
// make wave and reference it
make/O/N=10 $s1 = p^2
wave fwave = $s1
// print one of its values
print fwave[2]
return 0
end
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
December 9, 2014 at 03:04 pm - Permalink
December 10, 2014 at 06:42 am - Permalink
string s1 = sample+"_"+stringfromlist(0,templist)
to put the name of an existing wave in string s1, then the wave must exist in the current data folder in order for
wave f = $s1
to give a valid wave reference.
December 10, 2014 at 07:38 am - Permalink
This $ business is starting to make sense...
December 10, 2014 at 12:00 pm - Permalink
I have a minimal working project that has achieved many of my objectives, so thank you to everyone who has helped.
// templist is a string like "1;2;4p5;". film is just a name like "abc"
string film, templist
// calling this a variable allows my code to compile. but when i run, the error indicates that i'm trying to operate on a null wave.
// if i eliminate this variable j line, and instead change j to 2 in the loop, the function does what i want it to (adds 10 to each wave).
variable j = itemsinlist(templist)
// even if i swap j for (itemsinlist(templist)) (in parenthesis like the /n flag in the make command) i get the null wave error.
// the waves whose names i make with strings are already in the data browswer
variable i
for(i=0;i<=j;i+=1)
string s = film+"_"+stringfromlist(i,templist)
wave f = $s
f = f + 10
endfor
end
any idea what my error is? i seem to be treating the continue test in the loop incorrectly. can the continue test not be written as a variable?
December 10, 2014 at 03:23 pm - Permalink
(corrected to account for Chozo's comment)
string film, templist
// variables for the function are right next to function name
// lines indented for easier reading
// local variables
// I like them all on top
variable ic, npnts
string sf
// declarations here as needed
npts = itemsinlist(templist) // abbreviations make better sense than single letters
// for loop to do to work
for(ic=0;i<npts;ic+=1)
sf = film + "_" + stringfromlist(ic,templist) // spaces keep the line readable
wave fwave = $sf // did I say that single letters make me nervous?
if (WaveExists(fwave)!=0) // we better check that fwave exists in local folder
fwave += 10 // shorthand notation to add 10 to all points in the wave
else
print "No local wave " + sf
endif
endfor
return 0 // I always return something, even when it is nothing
end
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
December 11, 2014 at 05:46 am - Permalink
i<=j
will count on element too much.itemsinlist
will give the number of elements (e.g., '4'), so you would have to count from 0 to 3 to loop through all elements. But since4<=j
is also true, the loop counts one more, and the reference fails since there are no more strings. The condition needs to bei<j
. This problem happens occasionally when one forgets that counting starts from zero.December 10, 2014 at 04:48 pm - Permalink
The code is all tuned up now!
December 11, 2014 at 08:05 am - Permalink
Probably the simplest way to get the list of all waves in the Data Browser is to use following macro:
string a
a=wavelist("*",";","")
make/n=(itemsinlist(a))/T/o nm
nm[]=StringFromList(p, a)
endmacro
Then one can refer to names, which are elements of nm wave.
February 6, 2020 at 08:17 am - Permalink