Returning a wave from function
Erik_Kran
Hi,
I have edited the following code:
Function PP()
Make /O /N=100 toto
toto=X*2
duplicate /O toto tata
tata= gen_w(toto)
edit tata
End
Function gen_w(name)
wave name
duplicate /O name H
H=name/100
return H
End
Make /O /N=100 toto
toto=X*2
duplicate /O toto tata
tata= gen_w(toto)
edit tata
End
Function gen_w(name)
wave name
duplicate /O name H
H=name/100
return H
End
When I execute PP(), tata is filled with expected values. It works well.
From the Command Window, if I execute:
print gen_w(toto)
only the first value of the wave (0) is printed. I was expecting an editing of the full wave.
Why am I wrong?
Thanks for your help.
Erik
In modern Igor environments your example does not even compile. The reason is that the return statement of a normal function is giving back a single (scalar double-precision) number. If you want to return a wave (reference) you need the /WAVE flag. Try this:
Make /O /N=100 toto=X*2
duplicate /O toto tata
Wave workwave = gen_w(toto)
tata=workwave
edit tata
End
Function/WAVE gen_w(name)
wave name
duplicate /O name H
H=name/100
return H
End
Print will still give you just a single point, though (which you can choose now however). For example, to print the 10th point of the returned wave use:
print gen_w(toto)[10]
January 5, 2021 at 03:06 am - Permalink
And another mysterious thing:
executing:
Make /O /N=100 toto
toto=X*2
duplicate /O toto tata, titi
tata= gen_w(toto)
//titi=gen_w(toto)*2
End
Function gen_w(name)
wave name
duplicate /O name H
H=name/100
return H
End
modifies as well the existing titi file, but the command line in the code is now a comment, and this file should not change.
I guess there is an issue in my understanding of wave manipulation.
Erik
January 5, 2021 at 03:13 am - Permalink
In reply to In modern Igor environments… by chozo
Thank you. It works. There is however still the strange issue if the function is called twice. I mean, if the following code is executed:
Make /O /N=100 toto=X*2
duplicate /O toto tata,titi
Wave workwave = gen_w(toto)
tata=workwave
// Wave workwave1=gen_w(toto)
// titi=workwave1*2
End
Function/WAVE gen_w(name)
wave name
duplicate /O name H
H=name/100
return H
End
The titi file is modified by the program, but the corresponding lines that should update tit have been cancelled.
What is wrong?
Thanks.
January 5, 2021 at 03:21 am - Permalink
Never mind. Stupid mistake: titi is generated at each time with duplicate.
January 5, 2021 at 03:28 am - Permalink
Wave references (and other types of references) can be confusing when you first encounter them. I might suggest some reading:
DisplayHelpTopic "Accessing Global Variables And Waves"
DisplayHelpTopic "Wave References"
DisplayHelpTopic "Accessing Waves In Functions"
January 5, 2021 at 11:26 am - Permalink
Thank you!
January 5, 2021 at 12:31 pm - Permalink