![](/profiles/wavemetrics/themes/wavemetrics/logo.png)
Problem with SetFormula from a user defined function
![](/sites/default/files/styles/thumbnail/public/default_images/Artboard%201_1.png?itok=jeHOCIXy)
Hello,
I'm having problems with the usage of the SetFormula command in combination with a user defined function.
I want a wave to be updated when at least one of the parameters of the user-defined function changes.
My user defined function is:
string df
wave bin
variable xp,yp,zp
wave srcwv=$(df+"img_chunk")
variable xbin = bin[0], ybin = bin[1]
//nvar xp = $(df+"xp"); xp = round(xp)
//nvar yp = $(df+"yp"); yp = round(yp)
variable dim0 = dimsize(srcwv,0), dim1 = dimsize(srcwv,1), dim2=dimsize(srcwv,2)
variable xlower = max(0, min(ceil(xp - (xbin / 2)), dim0-1))
variable xupper = max(0, min(ceil(xlower + xbin), dim0-1))
variable ylower = max(0, min(ceil(yp - (ybin / 2)), dim1-1))
variable yupper = max(0, min(ceil(ylower + ybin), dim1-1))
matrixop /o tmp = sumcols(sumrows(subrange(srcwv, xlower,xupper,ylower,yupper)))
redimension /n=(dim2) tmp
return tmp
end
This should give me a 1d wave that can be assigned to an existing wave in another function using SetFormula.
For this purpose I used
execute "setformula "+df+"pz \"updateTransientV2("+ df +", round(xp), round(yp), round(zp), "+df+"bin)\""
in the other function so that "pz" is set to the returned wave.
I have searched for information about wave reference returns but I couldn't solve my problem.
To make clear what I need:
pz is a 1d wave. The function updateTransientV2 should calculate a new wave according to the parameters given and then return this to pz.
What am I doing wrong?
Thanks in advance
the expression in SetFormula returns values to be inserted in the named wave, not a wave.
If you need to recalculate all of the new values for the wave in one, you can set up a 'dummy' dependency to achieve that.
make some other wave or variable dependent upon a function in which you reset the wave pz.
w = v * x
return 1
end
•variable/G var1
•make/N=5 foo
•variable/G vDummy
•vDummy := func1(foo, var1)
•var1=3
•print foo
foo[0]= {0,3,6,9,12}
In this code, the dummy variable vDummy is explicitly dependent upon wave foo and variable var1, and the wave foo is effectively dependent upon variable var1.
I think that reentry is somehow blocked so that func1 is executed only once when var1 is changed, otherwise we might do something like:
wave w = $s
w = v * x
return 1
end
vDummy := func1("foo", var1)
I recommend that you try first to set up the dependency from the commandline using the := syntax to check how things work.
June 18, 2024 at 05:17 am - Permalink
I would suggest to avoid dependency formulas. Nowadays you can nearly always achieve your goal without dependencies, the latter are really hard to debug.
June 18, 2024 at 06:12 am - Permalink
Thanks for the quick replies!
I now used dummy variables that are dependent on a user function which update the desired waves in the course of the function. This works very well for my use-case.
Thank you Tony for the tips, it really helped!
But I see the point of Thomas, I was also kind of lost in the debugging process with dependencies.
June 19, 2024 at 12:06 am - Permalink