Understanding referencing?

Hi everybody,

I am still more or less new to Igor programming although I have been using Igor for a couple of years already. Here is my probably rather simple problem:

This is my working example code:

#pragma rtGlobals=3     // Use modern global access method and strict wave access.

Function test_analysis()

  string prefix, all_test    
  variable count, i
 
  prefix="test"
  all_test = WaveList(prefix+"*", ";", "")
  count = ItemsInList(all_test)
   
  for (i = 0; i < count; i+=1)
    wave test_p = $StringFromList(i, all_test)             
    wavestats/Q test_p
         
  endfor
     
End


Now, if I put the wave reference of "test_p" before the loop, it is not working anymore with the error message "Can't use "$" like this in a function".
Although that error already occured on multiple occassions in other projects of mine, I do not really know what is causing this in this context here. Here is the example code:

#pragma rtGlobals=3     // Use modern global access method and strict wave access.

Function test_analysis()

  string prefix, all_test    
  variable count, i
  wave test_p
 
  prefix="test"
  all_test = WaveList(prefix+"*", ";", "")
  count = ItemsInList(all_test)
   
  for (i = 0; i < count; i+=1)
    test_p = $StringFromList(i, all_test)              
    wavestats/Q test_p
         
  endfor
     
End


Furthermore, I just really like to know whether there is something like a "style guide" for Igor Programming (like it exists for C++ from google e.g.) because I could not really find related information in the manual and I think it is quite important. It would be just great to have some general "guide lines" for indentation, referencing and so on which would probably also make sharing Igor code easier.

Best regards,

Martin
Hi Martin,

so first to your problem. If you create a wave reference, you have to always use the  Wave keyword in front of the wave name. This is different from variables and strings where you can separate definition and assignment.
Due to this fact you can also have multiple wave statements with the same name.
#pragma rtGlobals=3     // Use modern global access method and strict wave access.

Function doStuff()

    Make/O data
    // Make implicitly creates a wave reference named "data"
    data = p
    Wave d1 = data
    print d1

    Make/O data2

    Wave d1 = data2
    print d1
End


And now if you use  wv  = $StringFromList(...) you are missing the wave keyword infront of wv. No matter if you use $ or a literal string.

Regarding the coding guidelines. I'm not aware of any official guidelines either.
I'm trying to follow my own rules:

  • Indent with one tab, a tab consists of four spaces (in case you are coding in a different editor)
  • variable names in camelCase (lower case first letter)
  • function names in CamelCase (upper case first letter)
  • Empty line after the function parameter declarations
  • All variables and strings are declared immediately at the beginning of the function after the empty line after the parameters. I do this mainly to remind myself that variables and strings don't have block scope as in C/C++.
  • Always use gui control procedures with the stucture parameter


See also my earlier post here.
If anyone is interested I can also post my gitconfig settings for igor code.
thomas_braun wrote:
...
I'm trying to follow my own rules:

  • ...
  • function names in CamelCase (upper case first letter)



I suspect then that perhaps you mean Function DoStuff() :-)

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
Addressing the WAVE statement issue...

A WAVE statement has two purposes, one at compile time and one at runtime. At compile time, it tells Igor's compiler that a given name (like test_p) refers to a wave so that the compiler can create wave code instead of, for instance, variable code. At runtime, it causes Igor to actually look up the wave with the given real name (the result of evaluating the string expression StringFromList(i, all_test)) and assigning a pointer to that wave to the wave reference test_p.

Many people with programming experience expect a WAVE statement to be like a C or C++ variable declaration. That is the compile-time part of a WAVE statement's job.

Many people fail to understand the run-time part of the WAVE statement. That is why your WAVE statement must be inside the loop- every time through the loop, it evaluates the string expression, finds the named wave and assigns the pointer to test_p. When you have a variable name that has been identified as a wave reference (via a previous WAVE statement) then any line that assigns to that variable must be a wave assignment statement, not a wave look-up. That is, it must be putting numeric values into the wave elements.

As far as indentation style goes, we have a preferred style that can be seen using the Edit->Adjust Indentation menu item. Select some code, then select that menu item to see what we prefer. As far as use of case, we don't have much of a preference, and everyone at WaveMetrics has their own style :) The Adjust Indentation menu item was a reaction to getting customer code that had NO indentation, and was nearly impossible to read...

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com