Simple Wave Extract

Hi all,
Here I'm trying to copy multiple 1-D waves into an INDEX-D wave, where the 1-D waves vary between 3000-4000 points. But somehow, the code keeps copying the last value of the 1D wave until it reaches 5000th.

make/O/N = (5000, index) SubWave
    Variable NewInd = 0
    Variable Col = 0
    do     
            Wave/Z w = WaveRefIndexed("", newInd, 4)    // Next wave in current data folder
        if (!WaveExists(w))
            break               // No more waves
        endif
 
            SubWave[][Col] = w[p]      // Copy wave w into SubWave
        NewInd += 1    
        Col  += 1
    while (1)


I also tried an alternative:

make/O/N = (5000, index) SubWave
    Variable NewInd = 0
    Variable Col = 0
    do     
            Wave/Z w = WaveRefIndexed("", newInd, 4)    // Next wave in current data folder
        if (!WaveExists(w))
            break               // No more waves
        endif
                Variable i =0
                do
           SubWave[i][Col] = w[i][0]    //copy the i-th row of w to the i-th row of the SubWave while the i-th row is not 'not a number'
                   i += 1
        while(numtype(w[i][0]!=NaN)        
                NewInd += 1    
        Col  += 1
    while (1)


This looks pretty obvious to me, but it doesn't work. Another solution is to get the number of data points of the wave I want to copy, and create a while-loop etc. Is there a way to get the length of a wave (something similar to 'strlen')? Thanks.
-Cheers
Igor's method for avoiding crashes caused by reading past the end of a wave is to return the last point of the wave when the index is past the end. If you change rtGlobals=1 at the top of your procedure to rtGlobals=3, that out-of-range index will cause an error (that's actually a Good Thing).

You don't have to write a loop, you can use subrange indexing on the left side:
SubWave[0,numpnts(w)-1][Col] = w[p]

This line assumes that w is a 1D wave. You can read more than you ever wanted to know about these topics by executing the following command:

DisplayHelpTopix "Waveform Arithmetic and Assignment"

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
Thanks! I also found a way to treat the problem. It's less elegant than the sub-range method, but it works.
At this point, I have one final problem: save my files in a "strange format"; something like this: dat.001, dat.002... dat.010, dat.011,...dat.999 (999 being the maximum number, Line 26) instead of dat.1, dat.2 etc. Any hints?Thanks

Function/S SaveAllNumeric2DWaveToTextFiles(pathName)
    String pathName     // Name of Igor symbolic path
    String fileNameList = ""
    Variable index = 0
    Variable name = 1
    do
        Wave/Z w1 = WaveRefIndexed("", index, 4)    // Next wave in current data folder ==ZSensor_Ext
        Wave/Z w2 = WaveRefIndexed("",index+1,4) //Next+1 wave in current data folder ==Defl_Ext
        if (!WaveExists(w1))
            break               // No more waves
        endif
 
        if (WaveDims(w1) != 1)
            index += 1
            continue            // 1D waves only
        endif
        index += 2                  //treat the next two files
       
        Variable i = 0             //<<<<<<<<<<<< LINE 17                                      
        Variable PTs = numpnts(w1)
        Make/O/N=(PTs,3) SubWave
        Do
            SubWave[i][0]=w1[i]*10^9     // ZSensor in nanometer is assigned to SubWave column 1
            SubWave[i][1]=0
            SubWave[i][2]=w2[i]*10^9     //Deflection in nm is assigned to SubWave column 3
            i += 1
        While(i<=PTs)           //<<<<<<<<<<<< LINE 25
       
        String fileName = "dat."+ num2istr(name)  // Convert integer 'name' into string (trying to get dat.001, dat.002 instead of dat.1, dat.2...)
        Save /P=$pathName /J /O  SubWave as fileName    // Save to text file
        fileNameList += fileName + ";" 
        name += 1
    while(1)
    printf "%03.0f\r" name   //<<<<<<<<<<<<< LINE 31
    return fileNameList
End  
Quote:
save my files in a "strange format"; something like this: dat.001, dat.002... dat.010, dat.011,...dat.999 (999 being the maximum number, Line 26) instead of dat.1, dat.2 etc.


Change this:
String fileName = "dat."+ num2istr(name)

to this:
String fileName
sprintf fileName, "dat.%03d", name


BTW, name is not a good name for a number.