Data-folder awareness for WaveList()

WaveList() is only returning results from the current data folder and thus needs acrobatics with SetDataFolder to work on different DFs. It is one of the few functions I use frequently which is not able to take a data-folder string or DFREF input. I propose the addition of this functionality (a new function WaveListDFR()?) to have this convenient function work with DFREFs.

I agree. This would help eliminate the gymnastics that one must sometimes undertake with SetDataFolder to jump around in data folders.

Yes, it would be great to have that. In the meantime I use this:

function /S WaveListDFR(matchStr, separatorStr, optionsStr, dfr)
    string matchStr, separatorStr, optionsStr; DFREF dfr
   
    DFREF saveDFR = getDataFolderDFR()
    SetDataFolder dfr
    string s=wavelist(matchStr, separatorStr, optionsStr)
    SetDataFolder saveDFR
    return s   
end

 

I think that new function should return a wave reference wave. And you have to ensure what you don't bug out, so something like
 

Function/WAVE WaveListDFR(string matchStr, string separatorStr, string optionsStr, DFREF dfr)

    string s
    variable err

    DFREF saveDFR = GetDataFolderDFR()
    SetDataFolder dfr

    try
        err = GetRTError(1)
        s = WaveList(matchStr, separatorStr, optionsStr); AbortOnRTE
    catch
        err = GetRTError(1)
        SetDataFolder saveDFR
        return $""  
    endtry

    WAVE wv = ListToWaveRefWave(s)
    SetDataFolder saveDFR

    if(DimSize(wv, 0) == 0)
        return $""
    endif

    return wv
End

based on tony's solution.

Thomas,

thank you for your suggestion and the code example. To be honest, I rather would get a string list as before from the function instead of a wave reference wave. I think a string list is easier to chop down further (e.g., by using GrepList()) than to handle wave references right away. Also, I wonder if in the code the ListToWaveRefWave(s) should come before the SetDataFolder command, otherwise the correct waves cannot be referenced, no?

@chozo. Thanks for the comment. Yes indeed the SetDataFolder must come later. And also handle an empty wave specially.

To create a wave reference wave you can already do something like below, but sometimes it's useful to create a stringlist directly, for instance if you need it for a popup menu.

DFREF MyFolder=root:MyFolder

//   Counts the number of waves in MyFolder
Variable n=CountObjectsDFR(MyFolder, 1)

//   Creates a list of all waves in the folder
Make/FREE/O/WAVE/N=(n) AllWaves=WaveRefIndexedDFR(MyFolder, p)

 

WaveRefIndexedDFR can be used to iterate through the waves in a particular data folder.