Execute and free waves

Is there a way to use free waves together with Execute? Let's assume I have two free waves wave1 and wave2 and wanted to do something like this:

Execute "wave1 = wave1[p] - wave2[p]"

Before you ask why I would want to use Execute for such a simple task: Of course I want to construct the command to be executed dynamically and drop the free waves in place as needed. I couldn't get anything to work, and my only idea is to drop any free wave into a temporary folder just for this task. Defeats the purpose of having free waves in the first place, so I wonder if this can work somehow.

Execute is like executing a command from the command line. It has access only to waves in the data hierarchy starting from root.

You could create a WAVE wave in the data hierarchy and store references to the free waves in the WAVE wave. You still have to create one wave in the data hierarchy.

 

Thanks for the quick answer. Yes, I guess so. I assume there is no other neat way to execute arbitrary commands like this other than building strings and using Execute? I will create a temporary folder in my case, since I only have two free waves currently.

I assume there is no other neat way to execute arbitrary commands like this other than building strings and using Execute?

I don't think so.

 

Something tangentially related I am curious about. I have now put the waves in a folder. But I found a behavior I do not fully understand. Usually you would need to provide the full path to Execute for it to work:

Execute "root:subfolder1:wave1 = root:subfolder1:wave1[p] - root:subfolder1:wave2[p]"

But I found that in cases where I call this from other code (associated to a panel) then it is enough to provide the wave names to the right side of the equation:

Execute "root:subfolder1:wave1 = wave1[p] - wave2[p]"

I wonder how this works, or in other words, how can Execute become aware of the wave location in some cases. I am happy that it works, but worry that I am relying on some quirk here which might disappear at some point. The code which leads to the call is rather convoluted and I have not been successful in extracting the essential ingredients so far, but I want to ask anyway in case somebody has an idea.

From help topic "More Wave Assignment Features"

The right-hand expression is evaluated in the context of the data folder containing the destination wave. See Data Folders and Assignment Statements for details.

Why not abuse SetDataFolder with a free datafolder?

Something like
 

Function Dostuff()

    DFREF currDFR = GetDataFolderDFR()
    DFREF dfr = NewFreeDataFolder()
    SetDataFolder dfr
    Make/N=10 wv1 = p
    Make/N=10 wv2 = p^2
    Make/N=10 wv3 = NaN

    Execute "wv3[] = wv1[p] + wv2[p]"

    SetDataFolder currDFR

    print wv3
End

should do the trick or?

Technically the waves are not free but local, see DisplayHelpTopic "Wave Tracking", but I don't think that is an issue.

Why not abuse SetDataFolder with a free datafolder?

Clever Thomas!

Although I think chozo will still need to create a global object (wave or variable) to get results back.

 

@Howard: Thanks a lot. I think that explains what I see here.

@Thomas: Thanks for the tip! I was trying free data folders actually, but without using SetDataFolder (it did not work). This approach only works when the destination wave is also inside the free folder, as I just learned from Howard, and not when the destination is a real wave inside a different (real) folder. Might come in handy in a different scenario, though.

not when the destination is a real wave inside a different (real) folder

This might do what you want:

// Execute from command line:
//  Make/O/N=3 root:OutputWave
//  Demo("root:OutputWave")
Function Demo(String fullPathToOutputWave)
    DFREF currDFR = GetDataFolderDFR()
    DFREF dfr = NewFreeDataFolder()
    SetDataFolder dfr
    Make/N=3 wv1 = p
    Make/N=3 wv2 = p+1
    Make/N=3 wv3 = 0

    Execute "wv3 = wv1 / wv2"
   
    Duplicate/O wv3, $fullPathToOutputWave  // This works
    WAVE wOut = $fullPathToOutputWave   // This also works
    wOut = wv3
    Print wOut

    SetDataFolder currDFR
End

 

Thanks. In my case I would Duplicate the output into the free folder to preserve all it's properties and then Duplicate it back out. But I guess, this can slow things down and is somewhat more memory intensive (I know, it is probably not too bad)? Since I only need the free waves in some cases I wonder which is less hassle to do for my current project, but good to know in any case!

@chozo: You can also use MoveWave for moving the wave into/from the free datafolder. This is faster and less memory intensive than Duplicate. But be sure to use IP 9.03 or later as there were some bugfixes with respect to MoveWave and free datafolders.