String list to wave labels


// this snippet takes a semicolon separated list and writes each element as dimension label in the required dimension.
//Example:
//Make/N=(2,3) M_w
//List2WaveLabels(M_w, "aa;bb;cc", 1)
//edit M_w.ld

function List2WaveLabels(w, str, dim)
	wave/Z w
	string str
	variable dim		// e.g. 0 for rows, 1 for colums
	
	variable nPnts = DimSize(w, dim)
	
	if (ItemsInList(str) != nPnts)
		printf "Error: Dimension %g of wave %s has %g entries but the string has %g.", dim, NameOfWave(w), nPnts, ItemsinList(str)
		return -1
	endif
	
	variable i = 0
	for(i=0; i<nPnts; i+=1)
		SetDimLabel dim, i, $StringfromList(i, str), w
	endfor
	
	return 1
end
Nice script!

Minor nit:
I would either use
Wave w
in the parameter declaration, or use
Wave/Z w
and later check for existence with

if(!WaveExists(w))
    return -1
endif

Forum

Support

Gallery

Igor Pro 10

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More