Return a complex variable help
BMangum
I thought I understood this, but I am having trouble somehow.
The real part works just fine, but the imaginary part is not being returned.
Any help with the code below would be appreciated.
FYI - I am using Igor Version 6.3.1.2 on Windows 7, maybe a bug?
#pragma rtGlobals=3 // Use modern global access method and strict wave access.
Function tester(sam, bill)
Variable sam
Variable bill
Variable/C dummy = cmplx(sam + 2, bill + 3)
Return dummy
End
Function Check()
Make/C/O/N = 100 wave0
Variable i
for(i = 0; i < 100; i += 1)
wave0[i] = tester(i, i*2)
endfor
End
Function tester(sam, bill)
Variable sam
Variable bill
Variable/C dummy = cmplx(sam + 2, bill + 3)
Return dummy
End
Function Check()
Make/C/O/N = 100 wave0
Variable i
for(i = 0; i < 100; i += 1)
wave0[i] = tester(i, i*2)
endfor
End
April 9, 2013 at 05:58 pm - Permalink
variable sam, bill, alex
Make/O/N=3 wOut = { sam + 2, bill + 3, alex + 4 }
Return wOut
end
Function Check3()
Make/O/N=(100,3) wave1
Variable i
for(i=1; i<100; i+=1)
wave wout = tester3(i, i*2, i*3)
wave1[i][0] = wout[0]; wave1[i][1] = wout[1]; wave1[i][2] = wout[2]
endfor
End
April 10, 2013 at 06:28 am - Permalink
Thanks, this gets around having to mess with complex variables all together.
April 10, 2013 at 09:04 am - Permalink
April 10, 2013 at 09:06 am - Permalink
The typical syntax is something like {out_object1,out_object2, ...} = funcname(inobject1,inobject2,...)
where out_object1, etc. could be anything (variable, wave, string, structure,...) and {...} is a list
This gets around the whole dance of using complex numbers (which only works for 2 numerical variables), which is a bad kluge (only works in 1 case, uses complex numbers for things that have nothing to do with complex numbers, etc.). Using global variables is also a bad solution, as it is far too easy to lose track of such objects, to overwrite them inappropriately, etc.
Pass by reference is a slightly better method but still far inferior to the straightforward solution proposed above. The issues are that there is no distinction in the syntax of calling, etc. between the input and output objects, making it less clear and more prone to programming slip ups.
The proposed solution also gets around the problem of creating programmatic names (extracting the NameofWave, creating a wave using $(), then creating a wave reference) and
leads to much cleaner code. In other words, you could write
Function funcname(w)
wave w
return {w+1,w+2} // example operation
End
rather than
wave w1,w2 // see how these "pop out of the blue"
Function funcname(w)
wave w
string ws=Nameofwave(w), ws1,ws2
ws1 = $ws+"1"
ws2 = $ws+"2"
duplicate /o w, $ws1, $ws2
wave wtemp1=$ws1, wtemp2=$ws2
wtemp1 = w+1 // example operations
wtemp2 = w+2
End
Sorry for the rant, but this is one area of Igor that badly needs improvement.
April 10, 2013 at 11:14 am - Permalink