Calling waves from data browser in loop

Hi,

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!
That would be via GetBrowserSelection().

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:
Variable index = 0
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
I would use something like:
Function test()

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


You could also use the 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.
Thank you for your suggestions. I shouldn't have listed the waves as sequential...they're not.

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.
Ok, so here's a bit of code that's doing simple versions of things I would like. I'm using the $ incorrectly, though. Below is what my intuition/experience using it have led me to expect, but it doesn't work! Can someone help me figure out my misconception?

function base(sample, templist)

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
Consider this instead. You must make the wave before you reference it.

function base(sample, templist, np)
    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
If you expect

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.
That helps a lot, and was one of the pieces I was missing.

This $ business is starting to make sense...
I'd like to keep asking questions regarding this project on this thread. Should I change the name? Or start a new thread?

I have a minimal working project that has achieved many of my objectives, so thank you to everyone who has helped.

function base(film, templist)

// 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?
Try this ...

(corrected to account for Chozo's comment)

function base(film, templist)
    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
The end condition in the for loop is wrong, as 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 since 4<=j is also true, the loop counts one more, and the reference fails since there are no more strings. The condition needs to be i<j. This problem happens occasionally when one forgets that counting starts from zero.
Thank you both for the style tips, safety coding checks, and solution.

The code is all tuned up now!

Probably the simplest way to get the list of all waves in the Data Browser is to use following macro:

macro g_n()

    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.