Simple syntax error in function declaration

Hi!

I am writing a simple function which find 3 1-D waves in current folder and take log transformation and save to another wave with log_* in the begining. When it is been compiled it has error that I cannot reference a wave like that [Can't use $ in this way in a function]. It looks like a simple syntax errror. Can some expert correct it or give me an alternative way to achieve this goal? Thanks!

Function GM1D_Log_qrs()
    NVAR currentWave, currentWaveType, currentWaveDim
    NVAR i,dim_q
     DFREF dfr = GetDataFolderDFR()
     Variable numWave = CountObjectsDFR(dfr, 1)
    SVAR list,wave_name_q, wave_name_r,wave_name_s
    SVAR wave_name
    SVAR name_here
    name_here = getdatafolder(0)

    // Get the list of waves in the current folder
     list = WaveList("*", ";", "DIMS:1")
     // Get the specific qrs waves in the current folder
     wave_name_q = WaveList("q_*", ";", "DIMS:1")
     wave_name_r = WaveList("r_*", ";", "DIMS:1")
     wave_name_s = WaveList("s_*", ";", "DIMS:1")
     dim_q=numpnts($wave_name_q)
     
    // Iterate through the waves and create new waves based on those starting with "q_"
    for (i=0; i<numWave; i+=1)
        wave_name = StringFromList(i,list)
       
        if ( (stringmatch(wave_name, "q_*") == 1) || (stringmatch(wave_name, "r_*") == 1))
            // Create a new wave with the same data as the existing wave
              string newWave = "log_" + wave_name
              make /O/R $newWave = log($wave_name) // this is where the error begin when it compile. Can't use $ in this way in a function???
        endif
        if ( (stringmatch(wave_name, "s_*") == 1) )
              string newWave = "log_" + wave_name
              make /O/R/N=dim_q $newWave = ($wave_name)/($wave_name_q)
       
    endfor
End

 

you need something like

wave foo  = $wave_name

to create a wave reference for the named wave

This compiles and I think it does what you want.

 

Function GM1D_Log_qrs()
    DFREF dfr = GetDataFolderDFR()
    Variable numWave = CountObjectsDFR(dfr, 1)
    String name_here = getdatafolder(0)

    // Get the list of waves in the current folder
    String list = WaveList("*", ";", "DIMS:1")
    // Get the specific qrs waves in the current folder
    String wave_name_q = WaveList("q_*", ";", "DIMS:1")
    WAVE existing_wave_q = $(wave_name_q)
    String wave_name_r = WaveList("r_*", ";", "DIMS:1")
    String wave_name_s = WaveList("s_*", ";", "DIMS:1")
    Variable dim_q=numpnts($wave_name_q)

    // Iterate through the waves and create new waves based on those starting with "q_"
    Variable i
    for (i=0; i<numWave; i+=1)
        String wave_name = StringFromList(i,list)
        WAVE existingWave = $(wave_name)
        string newWaveName
        if ( (stringmatch(wave_name, "q_*") == 1) || (stringmatch(wave_name, "r_*") == 1))
            // Create a new wave with the same data as the existing wave
            newWaveName = "log_" + wave_name
            make /O/R $newWaveName = log(existingWave) // this is where the error begin when it compile. Can't use $ in this way in a function???
        endif
        if ( (stringmatch(wave_name, "s_*") == 1) )
            newWaveName = "log_" + wave_name
            make /O/R/N=(dim_q) $newWaveName = (existingWave)/(existing_wave_q)

        endif
    endfor
End

I also changed the NVAR and SVAR statements to Variable and String, since I don't think you actually want to use globals for those variables.

In reply to by aclight

Thank you for your correction. I tried this code and compily works well. But it cannot work properly. As screenshot shown, Wave existing_wave_q is null. I have no clue how to fix this issue. So what others I can do?

error message:

While executing WAVE, the following error occurred: WAVE reference to "existing_wave_q" failed.

Capture_0.PNG
String wave_name_q = WaveList("q_*", ";", "DIMS:1")

WaveList returns a semicolon-delimited list of waves. In your screenshot example, it has found one wave, q_1_C.

Use StringFromList to extract a string from a list.

wave_name_q = StringFromList(0, wave_name_q) // set wave_name_q to first string in list
WAVE existing_wave_q = $(wave_name_q)

For your code to work reliably you must ensure that the required wave is the first wave that Wavelist finds.

Giory, I may be wrong, but I suspect that since you are using "qrs" naming system and are trying to take log of q and r waves, you are doing small angle scattering and may be either using my Nika or Irena packages. May be there are other packages in Igor which also generate qrs, in which case I'd love to know which (NIST code used "qis"). This naming system is old, very old, and really cumbersome and complicated to use.

In case you are using Irena, contact me directly (there is link in SAS menu to send me email) and I can help you to use either some of Irena tools directly or help you reuse some of the internal Irena functions. There is quite large and complicated package in Irena which supports data selection and would make your coding simpler - at cost of loading always Irena, though.