Get and Set "key-and-value" note metadata for waves
tkessler
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
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))
// 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"))
// 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
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
Forum
Support
Gallery
Igor Pro 9
Learn More
Igor XOP Toolkit
Learn More
Igor NIDAQ Tools MX
Learn More
September 28, 2013 at 12:06 am - Permalink
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.
September 30, 2013 at 03:22 pm - Permalink