Get and Set "key-and-value" note metadata for waves

Igor supports its "note" command for applying text metadata to a wave, but does not have an easy and intuitive way to organize data in it. Adding these two functions to your Igor procedures will give you a quick and intuitive way to add small "key-and-value" notes to any wave, allowing you to store a number of relevant data such as prior wave names, scaling factors, offsets, originating waves, and any other modifications made to the waves.

Keys and their values are separated by a colon in the wave note for easy organization, but these functions simply use a direct key and value reference to set or get the specified information.

Simply copy the following two functions to an Igor procedure file to make them available:

Function SetNote(wavenm, noteKey, newValueStr)
    WAVE wavenm
    string noteKey
    string newValueStr
   
    if(stringmatch(newValueStr,""))
        note/k wavenm, removebykey(noteKey,Note(wavenm),":","\r")
    else
        note/k wavenm, replacestringbykey(noteKey,Note(wavenm)," "+newValueStr,":","\r")
    endif
end

Function/S GetNote(wavenm,noteKey)
    WAVE wavenm
    string noteKey
    return stringbykey(noteKey,note(wavenm),":","\r")[1,inf]
end


The values are stored as text (required for use of the wave note), so for numerical variables you will need to use conversion routines like "num2str" and "str2num" when saving and retrieving the metadata, so if you have a wave called "myWave" and wish to tack on a recently applied scaling factor of 4.35, you would run the following:

SetNote(myWave, "Scaling Factor", "4.35")
// or...
SetNote(myWave, "Scaling Factor", num2str(4.35))


To retrieve this, you would run the following (optionally run the output of the following through "str2num" to treat it as a number):

string varStr=GetNote(myWave, "Scaling Factor")
// or...
variable var=str2num(GetNote(myWave, "Scaling Factor"))


To delete a note key and its value, simply set it to an empty string, such as the following:

SetNote(myWave, "Scaling Factor", "")


As an example of use in a function, you might include a loop like the following to dynamically retrieve stored scaling factors (in this case, traces in the topmost graph), and divide respective waves by them to reset a prior scaling calculation where the Scaling Factor was previously stored as a note, followed by removing the scaling factor note entry for the wave:

string traces //list of target wave names
string oldValue
variable i
for(i=0;i<itemsinlist(traces);i+=1)
    WAVE wavenm=tracenametowaveref("",stringfromlist(i,traces))
    oldValue=str2num(getnote(wavenm,"Scaling Factor")) //prior stored "Scaling Factor" note retrieved
    if(numtype(oldValue)==2)
        oldValue=1
    endif
    wavenm/=oldValue
    setnote(wavenm,"Scaling Factor","") //undone "Scaling Factor" is no longer needed, so it is deleted
endfor
I suppose it works, but what does it add to stringbykey,numberbykey,removebykey and replacebykey? Might as well use the standard functions, and use a custom function to display the contents in eay you like. Instead of getnote why not immediately call stringbykey?
Sure you can simply use the raw functions, but this stereotypes the wave note format and simplifies it. Its far easier to remember "getnote" and "setnote" if you use it frequently, than to remember to first stringbykey, then removebykey or replacebykey, etc., and manage the syntax of those. For instance, it's easier to simply do this...

string myStr = getnote(myWave,"Key Name")

...than it is to remember this:

string myStr = stringbykey("Key Name",note(myWave),":","\r")[1,inf]

The point of these functions is to simplify this process. I regularly create functions that append note data to waves, and using a simple setnote(waveName,"Key Name","value") syntax for storing, following by getnote(waveName,"Key Name") for retrieving is quick, intuitive, and works well. It also makes printing out the wave note to view all stored values easier to read, since this appends a carriage return to each line.

You also dont have to worry about whether to use replacebykey or removebykey, as you can do both of these with "getnote" by using a string value or by using an empty string. Again, this is just simplification, but I have found it to be quite handy.

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More