returning multiple variables
kperks
Thu, 05/12/2011 - 03:18 pm
I would like to return more than one variable and/or wave
function myfunc
...
return a, b, c
doesn't work. what is the correct syntax?
Thu, 05/12/2011 - 03:18 pm
Like most languages that trace their roots back to C, the Igor language doesn't provide for more than one return value. Instead what you probably want to use is pass-by-reference. Try executing
DisplayHelpTopic "Pass-By-Reference"
The same goes for functions that return waves: only one wave can be returned. Instead many Igor operations and functions support 'returning' more than one wave by creating waves with agreed-upon names. For example:
wave MyWave1, myWave2 // let Igor know that these are wave references pointing to waves with these names
// use MyWave1 and MyWave2 as you normally would
A third option to return multiple values is to simply return a single wave that contains them. You can even make use of waves-containing-wave-references to return multiple waves. But the latter can get confusing.
May 12, 2011 at 03:47 pm - Permalink
1. Pass a wavename into a function, and let that function create the wave
string wvName2 = "myWave2"
doStuff(wvName1,wvName1)
...
Wave wv1 = $wvName1
Wave wv2 = $wvName2
Function doStuff(str1, str2)
string str1, str2
Make $str1
Make $str2
End
This works also for waves in datafolders, just pass either a absolute path in str1, str2, or if they have a common folder use a dfref as additional parameter.
2. In case you don't need the last bit of performance (redimensioning is not as fast as creating it directly at the correct size) you can also do something like
doStuff(myWave)
...
// use the wave myWave
..
Function doStuff(wv)
Wave wv
Redimension/N=(desiredSize) wv
End
Especially the second example is quite nice with FREE waves.
May 13, 2011 at 01:46 am - Permalink
Like:
...
return num2str(a) + ";" + num2str(b) + ";" ...
End
Function get_a()
String funcvalues = myfunc()
print StringFromList(0,funcvalues)
End
May 13, 2011 at 06:13 am - Permalink
Make/D/FREE freewave={1,2,3}
return freewave
end
Function testdrive()
Wave w = test()
print w
end
Invoked from the command line:
•testdrive()
'_free_'[0]= {1,2,3}
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
May 13, 2011 at 09:39 am - Permalink
Even if the convenience outweighs the added cost, don't forget that you will generally lose precision in doing this:
0
May 13, 2011 at 05:00 pm - Permalink