Please explain this code
Amo
Hi Forum,
I'm trying to learn automation and programming in igor pro. I was looking to manual to see how to import data using procedure and came across this code from manual (page II164):
Function LoadAndGraphXY(fileName, pathName)
String fileName // Name of file to load or "" to get dialog
String pathName // Name of path or "" to get dialog
// load the waves and set the globals
LoadWave/J/D/O/P=$pathName fileName
if (V_flag==0) // No waves loaded. Perhaps user canceled.return -1
endif
// Put the names of the waves into string variables.
String sx0, sy0, sx1, sy1
sx0 = StringFromList(0, S_waveNames)
sy0 = StringFromList(1, S_waveNames)
sx1 = StringFromList(2, S_waveNames)
sy1 = StringFromList(3, S_waveNames)
Wave x0 = $sx0 // Create wave references.
Wave y0 = $sy0
Wave x1 = $sx1
Wave y1 = $sy1
SetScale d 0, 0, "s", x0, x1 // Set wave data units
SetScale d 0, 0, "V", y0, y1
Display y0 vs x0 // Create a new graph
AppendToGraph y1 vs x1
Textbox/A=LT "Waves loaded from " + S_fileName // Annotate graph
return 0 // Signifies success.
End
String fileName // Name of file to load or "" to get dialog
String pathName // Name of path or "" to get dialog
// load the waves and set the globals
LoadWave/J/D/O/P=$pathName fileName
if (V_flag==0) // No waves loaded. Perhaps user canceled.return -1
endif
// Put the names of the waves into string variables.
String sx0, sy0, sx1, sy1
sx0 = StringFromList(0, S_waveNames)
sy0 = StringFromList(1, S_waveNames)
sx1 = StringFromList(2, S_waveNames)
sy1 = StringFromList(3, S_waveNames)
Wave x0 = $sx0 // Create wave references.
Wave y0 = $sy0
Wave x1 = $sx1
Wave y1 = $sy1
SetScale d 0, 0, "s", x0, x1 // Set wave data units
SetScale d 0, 0, "V", y0, y1
Display y0 vs x0 // Create a new graph
AppendToGraph y1 vs x1
Textbox/A=LT "Waves loaded from " + S_fileName // Annotate graph
return 0 // Signifies success.
End
I understand most of the code, but I don't understanding why are they assigned to sx0 = StringFromList(0, S_waveNames), sy0 = StringFromList(1, S_waveNames) and so on, in particular what is (0, S_waveNames),
Thank you,
Hi,
The load function does not assign specific names to waves unless you are explicit. Additionally there are times if a name conflict occurs such as a preexisting wave, the name is appended with an incrementing suffix. For example if "fred0" exists as a wave then "fred1" is created. Which all that being said how do you know the exact names of the waves that have been loaded and created. That is where S_wavenames comes in, it is a string variable with a list of the waves created separated by semicolons,;.
The function then wants to do something with those waves, in this case setting the scale and adding them to an existing graph. Since an explicit reference to the waves is necessary, the "Wave" declaration is needed along with the name of the wave. Here X0 is a local name in the function and it is referencing the first wave created. The name is the first in the list in the string variable,S_waveNames. The function stringfromList(0,S_waveNames) means get the first item (since lists are 0 based) of the list S_waveNames and put it into a local string variable. Then using the $ operator use the value of that string for the wave declaration. Also as a note the two lines could be combined as one, but often separating them makes for easier following of code.
Andy
February 19, 2019 at 09:57 am - Permalink
Igor's operations often create variables to transmit information about what the operation did. The variables created are documented in the reference documentation for a given operation, usually at the bottom of the documentation for the particular operation.
Here are some links to help topics that may help you to understand how this works. Each is an Igor command- copy the DisplayHelpTopic command, paste it into Igor's command line and press Enter:
To learn about "operations" vs "functions": DisplayHelpTopic "Operation Commands"
To learn about variables created by operations: DisplayHelpTopic "Local Variables Used By Igor Operations"
The reference documentation for LoadWave: DisplayHelpTopic "LoadWave"
The LoadWave documentation is very long. You must scroll down a long way to get to the section called "Output Variables"
The reference documentation for the function StringFromList: DisplayHelpTopic "StringFromList"
Note that StringFromList has optional inputs for the list separator character- it defaults to ";". The list created by LoadWave uses the default, so the list separator character is not specified in this code.
February 19, 2019 at 10:41 am - Permalink
Your copy pasted example misses the return -1 in the V_Flag == 0 clause. I would also suggest to reindent the code by selecting the code in Igor Pro and choosing Edit->Adjust Indentation.
February 19, 2019 at 01:11 pm - Permalink
Guys, thanks a lot for your explanation.
February 20, 2019 at 12:07 pm - Permalink