How to get the length of a trace

Is there a way to get the length of a trace? Similar to what Numpnts or DimSize do for waves.

 

I am asking because I am building a data exporter that exports the data of traces plotted in a graph. Most of the time they will be 1D waves and even if the data used is from a 2D wave most of the time it will be the columns of such a wave. However, and maybe I am too far fetched here, I wanted to make it more flexible and make it able to export, traces printed from columns, but from rows, or even layers or chunks as well. In order to export the data I have to make a wave with the appropriate size, write the data into it and export it. I know I can get what is printed by using TraceInfo and reading XRange and YRange. However, getting the size is somehow complex because you would need to read the YRange keyword, check what has been plotted (row, column, layer, or chunk) and then get the size of that dimension of the wave with DimSize.

The misconception here seems to be the belief that traces are the same as data. They are not. Traces (and images) are representations of the data. The data are in the waves. What you will export is the data -- the waves -- not the representations of them -- the traces.

So, you need all the steps or the equivalent to what you describe to create a proper export method.

When your interest is to export the data as represented only by the portion that is displayed by the trace, you will need to use the information from the GetAxis operations to limit the range of data that you export from the waves to the traces.

I think I haven't explained myself well. I know that traces are not data, but sometimes the best way to decide what data you want to export or save is looking at their representations (traces in this case). That's why I have built an exporter that looking at the representation (traces) will be able to find the data (waves) and export it. The range of the wave which is being represented is easily found with Traceinfo, but in order to save the data corresponding to that trace I need to create a wave with the appropriate size. Reading the wave range of a trace is not that complicated but I just wanted to make sure it was not implemented already (I attached my not very elegant function to find the trace length).

 

I know Igor was specially designed for 1D data but sometimes it is useful to store different data in Matrixes and I thought it could be cool if I made my exporter insensitive to the way people store data (in rows, in columns, etc) although I know that in the vast majority of cases everybody will save data in columns if not in 1D waves.

 

FUNCTION TraceLength(WindowName, TraceName) // I do not add the instance parameter for simplicity
    String WindowName, Tracename
    //
    String Range
    Variable ini, fin, TLength
    //
    Range=StringByKey("YRange", TraceInfo(WindowName,TraceName,0))
    if(WhichListItem("[*", Range,"]") != -1) // if there is a [*] the whole dimension is plotted
        Wave WW=TraceNameToWaveRef(WindowName, TraceName)   // get wave
        return DimSize(WW, WhichListItem("[*", Range,"]"))  // get size of the appropriate dimension
   
    else //if there is no [*] only a subrange is plotted
        SplitString/E="\[.*," Range
        ini=Str2Num(RemoveEnding(S_Value[1, inf])) //Find start of subrange
        SplitString/E=",.*\]" Range
        fin=Str2Num(RemoveEnding(S_Value[1, inf])) // Find end of subrange
        TLength=fin-ini
        return TLength
    endif
END

In case you wanna check the function you can try with these two traces, and test the result of print TraceLength("", "WData") choosing which one is the top graph.

FUNCTION MakeTraces()
    // Make data and print trace
    Make/D/O/N=(10,11,12) WData=p+q+r
    Display/K=1
    AppendToGraph WData[1][2,4][1] vs WData[5][2,4][1] // print some random data
    Display/K=1
    AppendToGraph WData[1][][1] vs WData[5][][1] // print some random data
END

 

I think the answers to your questions would be No, as far as I am aware such a function has not been implemented and is not available built-in, and No, I don't think there is a better way to do it.