Sort after DimLabels

This snippet sorts columns or rows of a 2D wave after dimension labels in either Dim=0 or Dim=1. Should also work for 1D waves in Dim 0.

 

function SortDIML(wave w, int dim [, int rev])
   
    if(dim != 0 && dim != 1)
        return -1
    endif
   
    if(dim)
        MatrixTranspose w
    endif
   
    // make index wave(s) with DimLabels to sort after
    variable nPnts = DimSize(w, 0)
    Make/FREE/T/N=(nPnts,2) idxt
    idxt[][0] = GetDimLabel(w, 0,p)    
    idxt[][1] = num2str(p)
   
    // normal or reverse sort?
    if(!ParamIsDefault(rev) && rev !=0)
        sortcolumns/R/kndx={0} sortwaves={idxt}
    else
        sortcolumns/kndx={0} sortwaves={idxt}
    endif
   
    Make/FREE/N=(nPnts) idx, order = str2num(idxt[p][1])
    MakeIndex order, idx
   
    // insert idx column, sort and delete again
    Insertpoints/M=1 0, 1, w
    MultiThread w[][0] = idx
    Sortcolumns/DIML/kndx={0} sortwaves={w}
    Deletepoints/M=1 0, 1, w
   
    if(dim)
        MatrixTranspose w
    endif
    return 1
end


// Example:

Make/O/N=(3,3) w=p+q
SetDimlabel 1, 0, run3, w
SetDimlabel 1, 1, run1, w
SetDimlabel 1, 2, run2, w
edit w.ld
SortDIML(w,1)

 

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More