
Assigning a returned wave from a function without looping

Hello there,
I made a simple function that returns a sub-wave of the input wave. Reason for making it a function is to loop it over multiple 2D images and not create a mess in the experiment. Here is how it looks in use:
Make/N=(75, 35)/O test2d test2d = CreateSubwave2D_func(imageWave, 9.4, 10, 4, 5)[p][q] // The function simply returns a sub-wave of the input 2D wave with x-range from 9.4 to 10 and y-range for 4 to 5.
The problem is that during assignment to "test2d", the function is invoked for every pair of p and q, running the function 75*35=2625 times just to create one sub-wave (I think this is what's happening). Is there a way to assign the whole returning wave to "test2d" without looping?
Another issue demonstrated in the snippet above is that I have to create a wave with predetermined dimensions to assign the returning wave to. Is there a way to create a flexibly-dimensioned wave that adheres to the returning wave? Something like this:
Make/N=(10, 10) test2d test2d = CreateSubWave2D_func(imageWave, 9.4, 10, 4, 5) // the function returns a 75x35 wave. During assignment, test2d re-dimensions to match the returning wave's dimensions.
I can bypass all of this by using a macro instead, but as stated earlier it tends to create a lot of residue waves in the experiment which I have to kill manually, not being ideal for dealing with lots of data. An advice would be appreciated, thank you and happy Friday!
Do you really need to copy the returned wave? Why not something like
If really do need the copy, then do it in two lines:
March 24, 2023 at 10:27 am - Permalink
In reply to Do you really need to copy… by johnweeks
Hi johnweeks, I completely forgot I could do as simply as you've shown, I even used it in my previous code, thank you for the help!
March 24, 2023 at 12:33 pm - Permalink
I wonder if you can replace your function entirely by using the range parameters of the Duplicate operation?
This creates a wave named subset the is just the portion of the twod wave over the x range of 0.1 to 0.15, and the y range of 0 to 0.1.
March 24, 2023 at 06:40 pm - Permalink
Consider also using the /FREE flag in Duplicate or Make operations to create waves that will automatically go away when there are no longer any references to them.
March 25, 2023 at 06:44 pm - Permalink